Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,

I am trying to convert the date 6/26/2013 10:33:21 AM to 26/6/2013 10:33:21 AM. I tried this :


Dim dDate As DateTime = DateTime.ParseExact(str, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)

But i get a Format Exception. The string was not recognized as a valid Datetime.

Please help. Thanks.
Posted
Updated 26-Jun-13 9:33am
v4

Just switch the MM with dd in input format string, since your input date is in month/day/year (Jun, 26th) format.
 
Share this answer
 
Comments
Matt T Heffron 26-Jun-13 15:48pm    
+5 (great minds think alike!)
You were trying to parse with the format you wanted, not the format you have!
Try:
VB
Dim dDate As DateTime = DateTime.ParseExact(str, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
 
Share this answer
 
DateTime variables do not contain month first or day first type data. They contain numeric value that represents the date and time. When converting from DateTime format to String format, a "format" string is included to tell the Format function how to create the resulting string.

ParseExact requires that the number of characters match exactly. Parse allows single digit month or day.

VB
Dim str As String = "6/26/2013 10:33:21 AM"
Dim dDate As DateTime = DateTime.Parse(str, CultureInfo.InvariantCulture)
Dim strDayFirst As String = Format(dDate, "dd/MM/yyyy")


Tested Visual Basic .NET 2012
 
Share this answer
 
v4
Single "M" is not a known format code: try double
VB
Dim dDate As DateTime = DateTime.ParseExact(str, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
 
Share this answer
 
Comments
vidkaat 26-Jun-13 15:11pm    
I still get the same error.

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