Converting Datetime with AM/PM to 24Hr date format

Hi, I have a requirement where from the source file. I am getting date as string value ‘10/21/2019 6:16:54 PM’, I want the date to be converted into yyyy-mm-dd hh:mm:ss format.
In the example given above the output should be ‘2019-10-21 18:16:54’.
8/3/2019 4:24:00 AM for this input output should be 2019-08-03 04:24:00
for 7/3/2019 12:09:45 PM output should be 2019-07-03 12:09:45

I was able to achieve this using a complex regex but if anyone has a better way to do this please reply to this thread.


It looks like using uppercase H’s works:

yyyy-mm-dd HH:mm:ss{"format":"yyy-mm-dd HH:mm:ss"}) = 2020-16-10 16:16:57

Thanks Diane for taking time and responding, my problem however is when I do Date.parse($date) where $date = 10/21/2019 6:16:54 PM the result is NaN, since this string is not being parsed as date I am not able to use the toLocaleDateString function. I am using the below regex to achieve this

$Date.split(’ ‘)[1].split(’:’)[0]==‘12’? Date.parse($Date.replace(’ PM’,’’)).toLocaleDateTimeString({“format”:“yyyy-MM-dd HH:mm:ss”}):($Date.contains(‘PM’)? Date.parse($Date.replace(’ PM’,’’)).plusHours(12).toLocaleDateTimeString({“format”:“yyyy-MM-dd HH:mm:ss”}): Date.parse($Date.replace(’ AM’,’’)).toLocaleDateTimeString({“format”:“yyyy-MM-dd HH:mm:ss”}))


When using the Date.Parse() function to parse a string, you must also pass a “format” parameter, which specifies how your string is formatted.

parse(dateString, format) - Parse a date string using a custom format. The second argument is a format string using the same syntax as the Java SimpleDateFormat class.

Hi Chris
I tried what you mentioned in your comment, however I dont get the expected result, its not able to distinguish between AM and PM and gives same result for both

$dt = 11/12/2019 11:15:00 PM
expected output is 2019-11-12 23:15:00

Hi Chris,
Thanks for your inputs, I was able to achieve this by using the expression below,
Date.parse($dt,‘MM/dd/yyyy h:m:s a’).toLocaleDateTimeString({“format”:“yyyy-MM-dd HH:mm:ss”})


1 Like