By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

How to compare DateTime Objects?

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
Hi Krish,

Check out DateTime.Compare method !

// Jesper

Dec 21 '05 #2

P: n/a
<co*******@gmail.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.com>
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

P: n/a
<je************@yahoo.se> wrote:
Check out DateTime.Compare method !


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

--
Jon Skeet - <sk***@pobox.com>
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

P: n/a
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.AddDays(1);

TimeSpan ts = end - start;
Dave

<co*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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

P: n/a
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<je************@yahoo.se> wrote:
Check out DateTime.Compare method !


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

--
Jon Skeet - <sk***@pobox.com>
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

P: n/a
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.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.com>
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

P: n/a
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.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.com>
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

P: n/a
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.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.com>
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

P: n/a
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.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.com>
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

P: n/a
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote:
No, I think the answer if any would be parse with g and reformat with r.
That's what I said, isn't it?
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.
Does it actually record the time according to system local time? If so,
you can just use DateTime.ToUtc to get the UTC time.
I'll never know what time it was in the user's local time as a result.
Finding out the user's local time is always tricky.
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.


I'd certainly hope it's not getting it from the client. That's a
terrible way of doing things - just look what happens in Usenet. It's
likely to be server local time.

The only ways I know of converting to a user's local time are:
1) Get the user to tell you their timezone
2) Use JavaScript to convert from UTC to local time

--
Jon Skeet - <sk***@pobox.com>
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 #11

P: n/a
Not exactly sure what the question is here. If the server records in GMT
(00:00) (i.e. UTC) that is good. It does not matter what time zone the
server is actually in as long as it records in UTC. So normally, all
clients will send time in UTC so the server does not have to know the
client's time zone. Then the clients can make any required conversions for
display based on their local time. If the server, for some reason, needs to
work with the client's local time (i.e. reports, etc.) then you need to get
(or have stored) the client's time zone so that you can make the conversion
from UTC to local time *for the client. I have some code that will take a
user's standard time zone name and convert a UTC time to a local time at the
server. What are you trying to do?

--
William Stacey [MVP]

"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.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.com>
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 #12

P: n/a


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote:
No, I think the answer if any would be parse with g and reformat with r.


That's what I said, isn't it?


<snip />

Sorry to confuse. I was repeating what you said in effect agreeing to look
at it from a different perspective.
I always apprecite your comments Jon. I'm going to reply to William as he
indicates he has experience with the concern I have raised when using the
WAT.

<%= Clinton Gallagher

Dec 24 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.