Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a string variabl which contains current time (e.g 3:20 PM),i want to make an if condition like if(string > "9:15 AM") then do this else do something else,If i use this code it says that operator > cannot applied to strings

How can i convert this string to Time??
Posted
Comments
Rajesh Anuhya 6-Jan-11 5:26am    
Are you searched in Google???

DateTime.TryParse[^] is probably the best solution

Regards
Espen Harlinn
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 6-Jan-11 5:35am    
with DateTime it is checking date but i have to check time
Espen Harlinn 6-Jan-11 5:47am    
Once you have the DateTime, use DateTime.TimeOfDay. You can also use regular expressions and do the parsing "manually".
Hi,

try this quick and dirty solution:
C#
string str1 = "3:20 PM";
DateTime time1;
if(DateTime.TryParse(str1, out time1))
{
  //It is a Valid DateTime
  DateTime time2 = new DateTime();
  time2 = time2.AddHours(9);
  time2 = time2.AddMinutes(15);
  if (time1.TimeOfDay > time2.TimeOfDay)
    MessageBox.Show("Time is later");
}
 
Share this answer
 
Comments
Espen Harlinn 6-Jan-11 5:58am    
5+ Just like that :)
Nuri Ismail 6-Jan-11 6:04am    
5+ I like quick and dirty solutions :)
Here is the link for you

Here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900