Converting to Date format from string with no delimitors

I have a string with value "“20240509051859” which yyyyMMddhhmmss. I would want to convert it to “MM/dd/yyyy hh:mm:ss”. I tried using Datetime.Parse but it is not working.

Please help.

1 Like

Hi @nanmishra

Could you try this, please?

DateTime.ParseExact("20240509051859","yyyyMMddhhmmss",System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy hh:mm:ss")

Just one note about the format of your string:

Could you confirm if it is the correct format?

I believe that it is yyyyMMddHHmmss instead of yyyyMMddhhmmss.

hh: The hour, using a 12-hour clock from 01 to 12.
HH: The hour, using a 24-hour clock from 00 to 23.

If it is HH, please use:

DateTime.ParseExact("20240509051859","yyyyMMddHHmmss",System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy HH:mm:ss")

If it is hh, it’s missing the AM/PM. Otherwise it’s impossible to know if 05 is 05 AM or 05 PM:

DateTime.ParseExact("20240509051859 PM","yyyyMMddhhmmss tt",System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy hh:mm:ss tt")

If this solves your problem and you agree, kindly mark the post that helped you the most as solution to close this topic.

If you need extra help or have any questions, please let me know :slight_smile:

Thanks!

@gustavo.cervelin Thanks a lot for the help.
The format was with HH and the second DateTime that you provided worked.

1 Like

It’s a pleasure.

I’m glad it worked :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.