473,549 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare DateTime Objects?

Hi all..

Can anyone tell me how to compare datetime objects?I ve three
objects namely Current date,start date and end date.. I need to check
the current date with Start date and end date....Plz tell me how to
compare ???

Thanx..
Krish

Dec 21 '05 #1
12 61191
Hi Krish,

Check out DateTime.Compar e method !

// Jesper

Dec 21 '05 #2
<co*******@gmai l.com> wrote:
Can anyone tell me how to compare datetime objects?I ve three
objects namely Current date,start date and end date.. I need to check
the current date with Start date and end date....Plz tell me how to
compare ???


Assuming you're trying to check whether it's in the range start-end,
just use:

if (start <= current &&
current <= end)
{
// It's in the range
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #3
<je************ @yahoo.se> wrote:
Check out DateTime.Compar e method !


While you certainly can use DateTime.Compar e, the code will be more
readable if you just use the <, <=, > and >= operators.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #4
Krish,

In addition to the posting from the others, you might be interested to know
that you can also get the difference between the DateTime values by
subtracting them.

DateTime start = DateTime.Now;
DateTime end = DateTime.Now.Ad dDays(1);

TimeSpan ts = end - start;
Dave

<co*******@gmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi all..

Can anyone tell me how to compare datetime objects?I ve three
objects namely Current date,start date and end date.. I need to check
the current date with Start date and end date....Plz tell me how to
compare ???

Thanx..
Krish

Dec 21 '05 #5
Isn't there something to be said about the format of the dates and times
being compared with one another or any subsequent operations performed using
the given dates and times?

<%= Clinton Gallagher

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<je************ @yahoo.se> wrote:
Check out DateTime.Compar e method !


While you certainly can use DateTime.Compar e, the code will be more
readable if you just use the <, <=, > and >= operators.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 21 '05 #6
clintonG <cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:
Isn't there something to be said about the format of the dates and
times being compared with one another or any subsequent operations
performed using the given dates and times?


Not entirely sure what you're getting at. DateTimes aren't
intrinsically formatted, and they're immutable. Can you elaborate?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #7
Well, I'm wondering about values represented by or returned by a current
date, start date and end date as the OP asked about and started thinking
about comparison methods when formatters may have been involved.

Consider all the formatters in this cheat sheet [1] motivated me to wonder
if there is a "transformation " methodology that has been developed or can be
learned. How to transform from 'g' to 'r' for example?

<%= Clinton Gallagher

[1] http://www.dotnetjohn.com/runtime/DateFormats.aspx
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
clintonG <cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:
Isn't there something to be said about the format of the dates and
times being compared with one another or any subsequent operations
performed using the given dates and times?


Not entirely sure what you're getting at. DateTimes aren't
intrinsically formatted, and they're immutable. Can you elaborate?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 22 '05 #8
clintonG <cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:
Well, I'm wondering about values represented by or returned by a current
date, start date and end date as the OP asked about and started thinking
about comparison methods when formatters may have been involved.
Well, I'd hope that by the time the OP has the date/times as DateTimes
rather than strings (if they ever were strings) they'd be correct - so
whatever formatting has happened before *should* be irrelevant.
Consider all the formatters in this cheat sheet [1] motivated me to wonder
if there is a "transformation " methodology that has been developed or can be
learned. How to transform from 'g' to 'r' for example?


Parse with g and reformat with r? Sounds like the easiest way to me :)

I get the feeling I'm still missing your point.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 22 '05 #9
No, I think the answer if any would be parse with g and reformat with r.
I've got a real problem I've discovered and nobody has been able to tell me
the facts yet.

Using the 2.0 Website Administrator Tool e (WAT) addes new users to the
aspnet_Users table (Membership) in GMT(00:00) when the server the WAT runs
on is for example in GMT(-06:00). That is WAT records the time 6 hours into
the future. I'll never know what time it was in the user's local time as a
result.

I need to work with GMT as it is the format required by syndicated feeds but
I have no experience with conversions, comparsions and magic tricks like
trying to determine what the local time was when a user was added to the
Membership database.

I haven't even been able to learn if the WAT is recording time as would be
expected as I always assumed the "standard" was to record time using the
time of the server or getting it from the client and using that value.

<%= Clinton Gallagher

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
clintonG <cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:
Well, I'm wondering about values represented by or returned by a current
date, start date and end date as the OP asked about and started thinking
about comparison methods when formatters may have been involved.


Well, I'd hope that by the time the OP has the date/times as DateTimes
rather than strings (if they ever were strings) they'd be correct - so
whatever formatting has happened before *should* be irrelevant.
Consider all the formatters in this cheat sheet [1] motivated me to
wonder
if there is a "transformation " methodology that has been developed or can
be
learned. How to transform from 'g' to 'r' for example?


Parse with g and reformat with r? Sounds like the easiest way to me :)

I get the feeling I'm still missing your point.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7866
by: Grahammer | last post by:
Is it possible to compare two objects of the same class directly to each other? In the code below I'm comparing "Search" against "oneForm.thisForm"... Both of these objects are of the class "FormID". The IF statement never evaluates to TRUE even if the three strings in the FormID object match. Trying to use "=" instead of "IS" results in a...
3
2882
by: python | last post by:
Hi- I want to make a list of mx.DateTime objects that cover all the monthly intervals between two points. For example: >>> import mx.DateTime >>> nov1999 = mx.DateTime.Date(1999, 11) >>> mar2003 = mx.DateTime.Date(2003, 3) >>> alldates = mk_list(startdate=nov1999, enddate=mar2003)
4
5104
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and Regards, Nicolas
2
1680
by: Daniel | last post by:
how to get the number of milliseconds between two System.DateTime objects
1
9455
by: Ron | last post by:
How can I find the difference in seconds of two DateTime objects. I have looked at the Subtract method but this gives me back another DateTime object. Thanks, Ron
7
12865
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function...
4
1912
by: Lad | last post by:
How can I find days and minutes difference between two datetime objects? For example If I have b=datetime.datetime(2006, 8, 2, 8, 57, 28, 687000) a=datetime.datetime(2006, 8, 1, 18, 19, 45, 765000) Thank you for help L.
3
28468
by: Mark | last post by:
I'd like to compare two datetime values in milliseconds. The datetime.compare method appears to show only seconds. Milliseconds of a datetime are available as a property of each datetime, but I am assuming there is an easier way than comparing the milliseconds, seconds, minutes, hours, days, etc, individually to generate the difference. ...
2
1235
by: Daniel | last post by:
Is any .net updated required for the new daylight savigns time, or just update the OS is all that is needed? Do DateTime objects need to be updated in .net runtime?
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.