Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
need to compare the datetime with system time, but my datetime output is in (MM/dd/yyyy h:mm:ss tt) this format. so please help me compare with system datetime.

tried below step, bt not getting convert

DateTime dttime;
DateTime.TryParse("4/25/2014 6:44:00 AM", out dttime);
Posted
Comments
CHill60 8-May-14 7:23am    
And if it was successful then the result would be in dttime. TryParse is the better choice than Convert.ToDateTime as it handles exceptions better.

The system isn't recognising the date properly ... try explicitly forcing the date culture ...

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTime dttime;
if (DateTime.TryParse("4/25/2014 6:44:00 AM", culture, DateTimeStyles.None, out dttime))
{
     //use the date;
}
 
Share this answer
 
v2
TryParse returns a bool which says if it works - so it's worth checking it.
Try this instead:
C#
bool worked = DateTime.TryParseExact("4/25/2014 6:44:00 AM",
                                     "G", 
                                     new CultureInfo("en-US"),
                                     DateTimeStyles.None,
                                     out dttime);
 
Share this answer
 
Comments
CHill60 8-May-14 7:26am    
Overlap! But yours is neater AND uses TryParseExact. My 5
OriginalGriff 8-May-14 8:20am    
Why the name change? Bored? M*A*S*H fan?
CHill60 8-May-14 8:34am    
I'd forgotten about that! It was in response to a comment from Maciej ... he often calls me Hawkeye after I've spotted the one-character typos and things like that. So yes! I think we were bored yesterday, and yes, I'm a closet M*A*S*H fan!
DateTime.ParseExact (yourtime,"dd/MM/yyyy hh:mm:ss tt" ,CultureInfo.InvariantCulture );

change according your need...eg: MM/dd/yy h:mm:ss tt - 05/12/14 4:50:00 AM
 
Share this answer
 
TryParse results in a Boolean value that tells if the given string can be set as a date time value.
If that is False the out dttime will not have the desired value.

You might need the CultureInfo whle parsing: See here[^]

You can use a CultureInfo when trying to TryParse. Example:

C#
string dateString;
CultureInfo culture;
DateTimeStyles styles;
DateTime dateResult;

// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");  // US format -> Replace with yours!
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
   Console.WriteLine("{0} converted to {1} {2}.", 
                     dateString, dateResult, dateResult.Kind);
else
   Console.WriteLine("Unable to convert {0} to a date and time.", 
                     dateString);
 
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