tags:

views:

113

answers:

6

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
}
+1  A: 

You can use DateTime.Compare() along with DateTime.Parse() to convert the string to a DateTime object.

Shane Fulmer
No he can't because he doesn't have an instance of `DateTime`.
Darin Dimitrov
Yes he can, because strings can of course be parsed and thereby turned into `DateTime` objects. See e.g. the `DateTime.TryParse` and `DateTime.Parse` methods.
stakx
@stakx, `DateTime.Parse` was not part of this answer initially, at least not at the time I posted my comment.
Darin Dimitrov
A: 
if (time_now > Date.Parse(DBString)) {

} else {

}
Sukasa
+1  A: 

DateTime.Parse Will convert the string into a DateTime object which you can then use to compare.

John Weldon
+3  A: 

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.

LBushkin
+1, parsing is necessary.
Darin Dimitrov
A: 
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.

Allon Guralnek
I'm guessing that's a user input that the OP's validating - otherwise yes, lets hope that isn't from a database.
James Westgate
Before the question was edited, he explicitly said that he was getting a string form the database, and that string contained the time he wanted to use in the `if` statement.
Allon Guralnek
Actually, it still does.
Allon Guralnek
+1  A: 

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.

Hans Passant
Hans, that isn't actually a problem, since when passing a string that contains only a time to `DateTime.Parse(string)`, the method assumes the current date, so it can be compared to `DateTime.Now` without any issues.
Allon Guralnek
@Allon: ah, didn't realize that. I wonder of the next guy that maintains the code knows it too.
Hans Passant
The next guy probably won't spot that potential "gotcha" (I didn't), but if he has a keen eye like you and he does spot it, he'll wonder what the date will be set when parsing a string that only has the time, and find the answer when checking MSDN article for `DateTime.Parse(string)`.
Allon Guralnek