Hello Guys,
This is my string: “16/05/2018 08:19”
i am using (Date.ParseExact( str,“dd/MM/yyyy HH:mm”,system.Globalization.DateTimeFormatInfo.InvariantInfo)) .tostring(“yyyy-MM-dd HH:mm:ss”)
Hi,
why i am getting this error “String was not recognized as a valid datetime”
while passing hardcoded string its not showing any error.
message also showing correct time and date.
@smita.mobifly
buddy we need am or pm to get this in 24 hour format as 08:19 can be taken as both morning and night…but the compiler takes as morning
So try like this we will get the value, else you were doing great
Datetime.ParseExact(“16/05/2018 08:19 PM”,“dd/MM/yyyy hh:mm tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)
if i am passing hardcoded string like [Datetime.ParseExact(“16/05/2018 08:19 PM”,“dd/MM/yyyy hh:mm tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)]its working perfectly,
but if im passing [Datetime.ParseExact(generatedDate.tostring,“dd/MM/yyyy hh:mm tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)
Because your input date time string does not have suffix AM or PM and you are using it in parsing like “dd/MM/yyyy hh:mm tt” so it is not able to find the same formate to parse so you will get error if you will pass time with suffix mentioned it will not throw error.