I have string which contains a time (obtained from a DB):
string user_time = "17:10:03"; //Hours:minutes:seconds
DateTime time_now = DateTime.Now;
How do I compare this string to a DateTime? I'd like something like this:
if(time_now > user_time)
{
//Do something
}
else
{
//Do something
}
DateTime supports comparison, but first you need to parse the date-time string, DateTime.Parse() should suffice:
var dateTimeStr = "17:10:03";
var user_time = DateTime.Parse( dateTimeStr );
var time_now = DateTime.Now;
if( time_now > user_time )
{
// your code...
}
Bear in mind, that comparing dates/times sometimes requires awareness of time-zones to make the comparison meaningful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With