1

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

Coyolero
  • 2,353
  • 4
  • 25
  • 34
  • 2
    Probably because the time is 00:00:00. – dbasnett May 21 '13 at 16:57
  • 1
    http://stackoverflow.com/questions/7875259/how-get-a-m-p-m-from-datetime – Prageeth godage May 21 '13 at 17:07
  • When I tried your code, the `isValidDate` was false too. – Dave Michener May 21 '13 at 17:07
  • That's true, outputDate.ToString("HH:mm") returns 00:00. But AM/PM designator set an hour? or not? – Coyolero May 21 '13 at 17:09
  • @DaveMichener, please use `New CultureInfo("en-US")` instead of dtInfo. I just testes with framework 4.5 and it returns false, but using framework 2.0 `isValidDate` is true. – Coyolero May 21 '13 at 18:15
  • 1
    I can confirm your results, that the `isValidDate` is True for .Net 2, and false for .Net 4. However, and I think @dbasnett is right here, in the .Net 2 scenario, if you try the following: `outputDate.ToString("f")` you will get the following result: `Monday, January 14, 2013 12:00 AM`. So even though the TryParseExact was successful, .Net 2 apparently ignores the AM/PM portion and the time portion is set to midnight. Apparently, .Net 4 thinks this it is an error to specify the AM/PM in the mask without also including the time itself. – Dave Michener May 21 '13 at 18:56

1 Answers1

0

Correct me if I'm wrong but there's no such thing as a 'PM default time'. There's only the default time value if you don't specify a time at all ("00:00:00"), if you don't specify minutes and seconds ("hh:00:00") or if you don't specify seconds ("hh:mm:00"). If you want PM you need to give it a time to work with. It wouldn't know what you were talking about if you fed it "AM" as an input either, it just so happens that the default it uses when it understands the format but can't unambiguously parse it is an AM time so it looks like it understands.

Adrian
  • 2,825
  • 1
  • 22
  • 32