I have one date in string "18/07/2013 04:25:28 PM".How to convert this string to DateTime in c#.When I am trying to convert it into Date time I am getting an error "Input String is not in correct Date format"
Asked
Active
Viewed 4,577 times
-3
-
6Was asked trilion times. Try `ParseExact` or `TryParseExact` – Kamil Budziewski Jul 31 '13 at 08:18
-
Can you post the code you use to convert that string into a `DateTime`? – Frédéric Hamidi Jul 31 '13 at 08:18
-
@Marcus - only in "weird" countries. ;P (dd/MM/yyyy vs. MM/dd/yyyy fight! *popcorn*) – Corak Jul 31 '13 at 08:21
-
no code and you can easly find a solution in SO to this without asking – giammin Jul 31 '13 at 08:24
-
1@giammin, but thar requires effort, so.. – Nikita B Jul 31 '13 at 08:27
4 Answers
1
DateTime.ParseExact(
"4/4/2010 4:20:00 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Martijn van Put
- 3,243
- 16
- 17
-
-
I've changed the example. You must give the tt format with it. – Martijn van Put Jul 31 '13 at 08:21
0
DateTime d = DateTime.Parse("18/07/2013 04:25:28 PM");
IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);
DateTime a = DateTime.ParseExact("18/07/2013 04:25:28 PM", "dd/MM/yyyy hh:mm:ss tt", culture);
Added another mechanism...
Soner Gönül
- 94,086
- 102
- 195
- 339
Dr Schizo
- 3,646
- 4
- 32
- 71
0
You can try something like using DateTime.ParseExact using Custom Date and Time Format Strings
DateTime dt = DateTime.ParseExact("18/07/2013 04:25:28 PM", "dd/MM/yyyy hh:mm:ss tt", null);
Adriaan Stander
- 156,697
- 29
- 278
- 282
0
I propose you the following solution :
DateTime d = DateTime.ParseExact("18/07/2013 04:25:28 PM",
"dd/MM/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
To found the format string, I used Custom Date and Time Format Strings in MSDN
Joffrey Kern
- 6,291
- 3
- 26
- 25