5

I can't figure it out, where did I go wrong?

I got the following datetime string, and need to parse it to datetime:

string timestr = "1/20/2014 12:05:16 AM"

And I trying to parse it like this:

DateTime.ParseExact( timestr,
                     "MM/dd/yyyy hh:mm:ss tt",
                     null);

When trying to do this it returns

"string was not recognized as a valid DateTime"

Any tip?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jason94
  • 13,320
  • 37
  • 106
  • 184

5 Answers5

12

MM is for 01 to 12

Use M instead which is for 1 to 12.

string timestr = "1/20/2014 12:05:16 AM";
var date = DateTime.ParseExact(timestr,
                               "M/dd/yyyy hh:mm:ss tt",
                               CultureInfo.InvariantCulture);
Console.WriteLine(date);

Output will be;

1/20/2014 12:05:16 AM

Here a demonstration.

For more information, take a look at;

Also be careful about your hour formatting. hh is for 01 to 12, HH is for 00 to 23. If your hour will be 13, 14 or 15 etc.. hh format will fail.

And since you using null as a IFormatProvider in your DateTime.ParseExact method, that means it uses CurrentCulture by default. And if it's DateSeparator is not /, your method throws FormatException even your string and format matches exactly because "/" format specifier has a special meaning in custom date and time formats like; replace me current culture's or suplied culture date separator

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Did you try

 DateTime returnedDate = new DateTime();
 DateTime.TryParse(timestr, out returnedDate);
Mohamed Salah
  • 959
  • 10
  • 40
  • If OP's `CurrentCulture` doesn't accept `M/dd/yyyy hh:mm:ss tt` as a standard date and time pattern, your `DateTime.TryParse` returns `false` and your `returnedDate` will be `01/01/0001 00:00:00`. – Soner Gönül Aug 29 '14 at 07:45
0

M-The month, from 1 through 12.

The "M" custom format specifier represents the month as a number from 1 through 12 (or from 1 through 13 for calendars that have 13 months). A single-digit month is formatted without a leading zero.

DateTime.ParseExact( timestr,"M/dd/yyyy hh:mm:ss tt",CultureInfo.InvariantCulture);

Msdn

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
0

PLease try

string timestr = "1/20/2014 12:05:16 AM";
DateTime dt = new DateTime();
DateTime.TryParse(timestr, out dt);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
rajeev.harbola
  • 83
  • 1
  • 10
  • 1
    Please don't just post code, please explain to the user what the code does so they can learn from it, as is the reason they have come to stackoverflow – Simon McLoughlin Jan 20 '14 at 09:42
  • 1
    You don't _have to_ initialize `out parameter` by the way. `DateTime dt;` will be enough. – Soner Gönül Jan 20 '14 at 11:09
  • If OP's `CurrentCulture` doesn't accept `M/dd/yyyy hh:mm:ss tt` as a standard date and time pattern, your `DateTime.TryParse` returns `false` and your `dt` will be `01/01/0001 00:00:00`. – Soner Gönül Aug 29 '14 at 07:44
-2

Try this one:

this.RequestDate = Convert.ToDateTime(this.DcmCreateDate).ToString("dd/MM/yyyy");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
codelikeprogrammerwoman
  • 1,429
  • 3
  • 14
  • 17