Click here to Skip to main content
16,014,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I am trying to convert string to datetime but it's giving error. My string format is "05/27/2016 8:00 AM" want to convert in datetime.

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Error: "String was not recognized as a valid DateTime."

I tried in different ways still showing same error. Please help me.


Thanks in Advance.

What I have tried:

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Posted
Updated 1-Jun-16 1:48am

This is working as well:
C#
DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy H:mm tt", System.Globalization.CultureInfo.InvariantCulture);


For further information, please see: DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^]
 
Share this answer
 
try this

C#
DateTime dt1 = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);


Quote:
dd => Day 28
MM => Month 05
yyyy=> Year 2016
h => hours 8
mm => minutes 0
tt => AM/PM
 
Share this answer
 
C#
DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);


Can "05/27/2016 8:00" be mapped exactly to "dd/MM/yyyy"? No it can't hence the error. If you want to use the dd/MM/yyyy parse format your string has to match that format, so remove the time from your string.

C#
DateTime dt = DateTime.ParseExact("05/27/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture);


Will that work? Still no. You are saying "05" is the day and "27" is the month, there is no 27th month. So make your date string match the format like this

C#
DateTime dt = DateTime.ParseExact("05/27/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);



Or use the proper format for the string you have, like this

C#
DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);



Custom Date and Time Format Strings[^]

If what you're trying to do is create a DateTime object where the date is a specific format (the one you have in your format string) then dates don't work like that, they don't have a format until you convert them to a string. So you convert your string to DateTime and that DateTime represents that moment in time, and if you want to display that moment in time in a certain format you use ToString on the DateTime and specify the format you what the DateTime represented as.
 
Share this answer
 
ParseExact says "I want it this way, and no other" - so when you provide format string that doesn't exactly match you input, it will fail. And ... your month is first, not second...
Try:
C#
DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/d/yyyy h:mm tt", CultureInfo.InvariantCulture);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900