DateTime manipulation

T

Tim Cowan

Hi,

In my application I need to compare whether one time is greater than
another. What I am doing right now is taking the current datetime,
formatting as string without the time element, and then appending the time I
want to the date and converting to date.

DateTime dOpen = System.Convert.ToDateTime(now.Date.ToString("MMM dd, yyyy")
+ " " + storeOpen.Hour + ":" + storeOpen.Minute);

Is this the best way or is there another way? I am then comparing dOpen to
DateTime.Now to see if it is more or less. What I want to know is, is the
store open according to the time right now. If current datetime is greater
than dOpen then it is.

Tim
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

I think the problem here is that you have the time the store is open as
a time, and not really a date. You don't store every date that the store is
open, just the general hours. This information is in a TimeSpan.

Then, you have the current date. What I think you should do is create
the current date the store opens for the day you are comparing. So, assume
you have the time the store opens in a TimeSpan, like so:

TimeSpan storeOpen;

And then you get the current date.

DateTime now = DateTime.Now;

Once you have that, you just need to know when the store opens today:

DateTime storeOpensToday = (now - now.TimeOfDay) + storeOpen;

Then, you can compare that against now:

if (now >= storeOpensToday)
{
// Perform your work.
}

Hope this helps.
 
G

Guest

Look up the instance DateTime.CompareTo() method, or the static
DateTime.Compare() method. That is the best way for comparing dates and
DateTimes.

That said, if all you need to do is compare the time, with the date portion
being insignificant, then you can use something like this:

int result = myDateTime.TimeOfDay.CompareTo(myOtherDateTime.TimeOfDay);

If you use the < or > or other comparison operators to compare your
DateTimes the framework calls DateTime.Compare() so in the simplest for then,
just use this:

bool IsLessThan = myDateTime.TimeOfDate < myOtherDateTime.TimeOfDay;

HTH
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
I think the problem here is that you have the time the store is open as
a time, and not really a date. You don't store every date that the store is
open, just the general hours. This information is in a TimeSpan.

Then, you have the current date. What I think you should do is create
the current date the store opens for the day you are comparing. So, assume
you have the time the store opens in a TimeSpan, like so:

TimeSpan storeOpen;

And then you get the current date.

DateTime now = DateTime.Now;

Once you have that, you just need to know when the store opens today:

DateTime storeOpensToday = (now - now.TimeOfDay) + storeOpen;

Then, you can compare that against now:

if (now >= storeOpensToday)
{
// Perform your work.
}

Note that it's important to only take DateTime.Now *once* (as Nick has
done above). If you use it twice, it's possible that the day will
change between calls!

I'd use now.Date rather than (now - now.TimeOfDay) though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top