0

Hi,

I have time from DB:
string user_time = "17:10:03"; //Hours:minutes:seconds
DateTime time_now = DateTime.Now;

How do I compare the time?, How make this:

if(time_now > user_time)
{
    //Do something
}
else
{
  //Do something
}

Thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by apegram
0

A time in the format hh:mm:ss will parse into a date as today's date plus the parsed time. So 17:10:03 would parse as April 27, 2010 5:10:03 PM.

With that in mind, you would be able to simply do a straight comparison of your parsed time value and the currrent date and time.

As for conversion methods, I would recommend DateTime.TryParse(input, out result) on your string.

string input = "17:10:03";
DateTime userTime;
if (DateTime.TryParse(input, out userTime))
{
    // compare user time to datetime.now
}
else 
{
    // invalid input
}

As a side note, I didn't expect that behavior on the parsing of a time sans a date. I expected it to be something like "1/1/0001 5:10:03 PM." But I've tested and got the same unanticipated behavior in C# 3 and C# 4. I assume there's some documentation out there, but I have not conducted a proper search.

Edited by apegram: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.