I am trying to Parse a String date and time into a single .NET DateTime object. I have the following code:
string dtObjFormat = "dd MMM YYYY HH:mm";
string mydatetimemash = e.Date + " " + e.Time; // this becomes 25 May 2013 10:30
DateTime dt;
if (DateTime.TryParseExact(mydatetimemash, dtObjFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
} else
{
dt = DateTime.Now;
Console.WriteLine(dt);
}
But the TryParseExact always returns false for me, meaning the Parse fails. What am I doing wrong?
Your Y's need to be lower case, like so:
string dtObjFormat = "dd MMM yyyy HH:mm";
string mydatetimemash = e.Date + " " + e.Time; // this becomes 25 May 2013 10:30
DateTime dt;
if (DateTime.TryParseExact(mydatetimemash, dtObjFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
} else
{
dt = DateTime.Now;
Console.WriteLine(dt);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.