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 same like this:
if(time_now > user_time)
{
//Do something
}
else
{
//Do something
}
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 same like this:
if(time_now > user_time)
{
//Do something
}
else
{
//Do something
}
You can use DateTime.Compare() along with DateTime.Parse() to convert the string to a DateTime object.
DateTime.Parse Will convert the string into a DateTime object which you can then use to compare.
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 (DateTime.Now > DateTime.Parse(user_time))
{
...
}
But you really shouldn't store a time as a string, you should use the native time or datetime format of your database, that way you could use the value of the time in your queries, and index them properly.
The problem is that DateTime.Now includes a date, "17:10:03" doesn't. Do it like this:
Dim dbaseTime As TimeSpan = TimeSpan.Parse("17:10:03")
If DateTime.Now.TimeOfDay > dbaseTime Then
Console.WriteLine("Let's go home")
End If
Do everything in your power to convert that string column type to a datetime column.