473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 61178
Hi Krish,

Check out DateTime.Compare method !

// Jesper

Dec 21 '05 #2
<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
<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
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
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
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
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
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
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
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
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


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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...
3
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) >>>...
4
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...
2
by: Daniel | last post by:
how to get the number of milliseconds between two System.DateTime objects
1
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
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...
4
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,...
3
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...
2
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.