[How to convert time string into 24 hrs format]

My String is dateError = “06/11/2021 7:00:18 PM”

Trying to convert it to 24 hrs Format using below;
Datetime.ParseExact(dateError,“dd/MM/yyyy hh:mm:ss tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

But i am getting error as “String was not recognized as a valid DateTime.”

image

May I know what I did wrong here.

Hi!

Try using Datetime.ParseExact(dateError,“dd/MM/yyyy h:mm:ss tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

Your date already has AM | PM, so no “hh” is required, just “h”

Hope it works for you!

h and hh does not has to anything with AM and PM.

  1. h → 12-hour clock hour (e.g. 4).

  2. hh → 12-hour clock, with a leading 0 (e.g. 06)

@spdhoke - Please check this…

Date.Parse(“06/11/2021 7:00:18 PM”).ToString(“MM/dd/yyyy HH:mm:ss”)

image

True, however a date won’t parse if you use 24 hour clock and AM | PM

24 hrs clock is different – that HH and H…User did not used that…Please check this…i have used hh and it still worked when there is a leading zeros…

Since the hour is 7 (not 07) we should not use hh.

1 Like

That is not the intended output…please check the requirement again…

So change the format at the end to what you want.

Date.Parse(“06/11/2021 7:00:18 PM”).ToString(“yyyy-MM-dd HH:mm:ss”)