I'm using this code to convert a string to date.
Dim outputDate as Date ' I have tried also with DateTime
Dim inputStringDate as String = "January 14, 2013 PM"
Dim dateFormat as string = "MMMM dd, yyyy tt"
isValidDate = Date.TryParseExact(inputStringDate, dateFormat, New CultureInfo("en-US"), _
Globalization.DateTimeStyles.None, outputDate)
' isValidDate = true
outputDate.ToString(dateFormat) 'January 14, 2013 AM
The issue is that "tt" is not recognize. The input text is "PM" (January 14, 2013 PM) but after parsing to date and applying it format I got "AM" (January 14, 2013 AM).
The time of outputDate
is 00:00:00, but I think it should be the PM default time 12:00:00.
Environment: .Net 2.0
dtinfo.PMDesignator = PM
dtinfo.AMDesignator = AM
Thanks for your help
Overview
When trying to parse a date string with AM/PM designator (tt) but without time (hh:mm), for example “January 14, 2013 PM” (MMMM dd, yyyy tt), it seems that .NET Framework 2 would successful parse it but would ignore the AM/PM portion by setting the time as AM default, this is midnight (00:00). The solution is always giving it the time e.g. “hh:mm tt” or “HH:mm”.
But using .Net Framework 4 in the same scenario, parsing the date string without time would fail; this is because .Net 4 would require the time and AM/PM designator (hh:mm tt), or the use of 24 format (HH:mm). Thanks to @Dave Michener for this (please review its comment)
So in .Net 4
MMMM dd, yyyy tt --> Fails
MMMM dd, yyyy hh:mm --> Fails
MMMM dd, yyyy hh:mm tt --> Works
MMMM dd, yyyy HH:mm --> Works