3

How to populate C# DateTime object from this "03-06-2012 08:00 am" string.

I'm trying some code of follwoing type:

DateTime lectureTime  = DateTime.Parse("03-06-2012 08:00 am");

I am using jQuery based this http://trentrichardson.com/examples/timepicker/ plugin to generate date time.

Update --

So many answers below and lot of stuff to clear basics for this small issue From the below snapshot u can see what I tried and what i received during debugging in visual studio enter image description here

Abhi
  • 5,501
  • 17
  • 78
  • 133

3 Answers3

3
string lectureTime = "03-06-2012 08:00 am";
DateTime time = DateTime.ParseExact(lectureTime , "dd-MM-yyyy hh:mm tt", CultureInfo.InvariantCulture);
  • dd: days [00-31]
  • MM: months [00-12]
  • yyyy: years [0000-9999]
  • '-': these are separated with a dash
  • hh: hours [00-12]
  • mm: minutes[00-60]
  • tt: time [am, pm] (case insensitive)
Nick Babcock
  • 6,111
  • 3
  • 27
  • 43
  • 3
    I suspect it should be `hh` rather than `HH` - you'd rarely specify a 24 hour clock *and* an AM/PM designator. – Jon Skeet Jun 03 '12 at 15:55
2

If you have the correct culture, your code works without modification. But you may be using a different date formatting from the program that generated the string.

I'd recommend always specifying a CultureInfo when:

  • Parsing a DateTime generated by another system.
  • Outputting a DateTime that will be parsed by another system (not just shown to your user).

Try this:

CultureInfo cultureInfo = new CultureInfo("en-GB"); // Or something else?
DateTime lectureTime  = DateTime.Parse("03-06-2012 08:00 am", cultureInfo);

See it working online: ideone

Difference between DateTime.Parse and DateTime.ParseExact

If you want .NET to make its best effort at parsing the string then use DateTime.Parse. It can handle a wide variety of common formats.

If you know in advance exactly how the dates should be formatted, and you want to reject anything that differs from this format (even if it could be parsed correctly and without ambiguity) then use DateTime.ParseExact.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • in this demo it replaced - with / in output, will the generated output string format will be recognized by sql datetime datatype? – Abhi Jun 03 '12 at 15:56
  • Actually, this answer is incorrect. `CultureInfo.InvariantCulture` internally assumes the U.S. date format (`MM/dd/yyyy`), so it will parse the given string as 6th March, not 3rd June (as required by the OP). – Douglas Jun 03 '12 at 16:01
  • @Douglas: It shoudl work if the culture is specified correctly. Maybe it's an Indian format, since the OP is from India? – Mark Byers Jun 03 '12 at 16:03
  • Yes. Practically all European cultures use `dd/MM/yyyy` (only the U.S. uses the middle-endian format). I’ve never seen it used with `-` as the date separator though. – Douglas Jun 03 '12 at 16:06
  • @Douglas if you use invariant you should specify which culture it is in the project, even if you are using the default. Anythng else is an invitation to to the ambiguity you noticed. – Tony Hopkinson Jun 03 '12 at 16:10
  • @MarkByers: AFAIK, Italy uses either `/` or `.`. But some regions of India do seem to use `-`. – Douglas Jun 03 '12 at 16:13
  • @Douglas: eastern europe uses - – Wiktor Zychla Jun 03 '12 at 16:23
1

You need to use DateTime.ParseExact. Something like

DateTime lectureTime  = DateTime.ParseExact("03-06-2012 08:00 am", "dd-MM-yyyy hh:mm tt", CultureInfo.InvariantCulture);
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
  • [This](http://stackoverflow.com/a/1009466/1103571) answer uses `ParseExact` for a `DateTime` string similar to the one in the question. – Lilienthal Jun 03 '12 at 15:51