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
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
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.