String type to DateTime type conversion : Format Exception


#1

I have a string in dd/mm/yyyy format. I just need to convert it to DateTime type.
No success using below,
Convert.ToDateTime
DateTime.Parse
DateTime(ParseExtract(“15/6/2018”, “dd/MM/yyyy”,System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)

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

Please help, Thanks!


#2

Hi,

Your input string is in format “dd/M/yyyy” that’s why it throws invalid datetime exception.
You can change the string format as “dd/M/yyyy”.

DateTime.ParseExact(“15/6/2018”,“dd/M/yyyy”,System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)

You can refer this link for more format options:
Custom Data and Time string format


#3

Thanks Bharat!