Convert 12 hour (AM/PM) string to 24 Date object using moment js
Asked Answered
L

10

67

I have a output resulting from a timepicker giving 12 hour format of time.

Eg : "1:45 AM (or) "12:15 PM" as **string**

Is there a way to parse this string format to 24 hour using moment js back to date object?

Leavis answered 6/11, 2014 at 15:25 Comment(3)
Does it output a date too? Don't think you'll be able to do much with just the time as it doesn't represent a valid date.Armoury
It just outputs a string of time in 12 hour format. But hou can this be converted to 24 hour format of Date object?Leavis
Can you please mark my anwer as accepted if it worked?Zanze
Z
150

See documentation of moment js parse function

JSFiddle

var dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");
Zanze answered 6/11, 2014 at 15:34 Comment(1)
but i am using hungarian locale. but i got same value without converting moment("02:15 PM", ["h:mm A"]).format("HH:mm"); // 02:15Balky
C
21

I know that this answer is not for the question (actually it is for the opposite case), but I will put it for completeness and if someone (like me) was looking for it.
In case you want to convert from the 24 Hour system to 12 Hour system then you could use the following

return moment("13", ["HH"]).format("hh A");

the previous code will produce the result 1 PM

Certification answered 17/12, 2016 at 8:22 Comment(1)
You can make it more reliable by [HH.mm]. ``` moment("1.12", ["HH.mm"]).format("hh:mm a"); => "01:12 am" ```Anoxemia
A
20

Just a little conversation "2 PM" to "14.00"

const number = moment("02:00 PM", ["h:mm A"]).format("HH:mm");
console.log(number); // Output = "14.00"

"14.00" to "2 PM"

const number = moment("14.00", ["HH:mm"]).format("hh:mm a");
console.log(number); // Output = "02:00 pm"
Anoxemia answered 19/11, 2019 at 9:13 Comment(1)
please fix a syntax error on the second code , it should be 'HH:mm' instead of HH.mmDepoliti
G
2

var today = new Date();
let options = {  
    hour: "2-digit", minute: "2-digit"  
};  
console.log(today.toLocaleTimeString("en-us", options));
Galosh answered 22/8, 2019 at 7:15 Comment(2)
Your code outputs time in AM/PM format. OP wanted to convert AM/PM to 24h, like "01:00 PM" to "13:00"Bedbug
add flag "hour12: false" like this {hour: "2-digit", minute: "2-digit", hour12: false }Locris
R
1

In full calender to show time in 12 hour format like 01:00 PM i used following format - timeFormat: 'hh:mm A'. "timeFormat" is the property of events of fullcalender.

Same format 'hh:mm A' we can use to format datetime object using momentjs.

Retentivity answered 16/9, 2016 at 8:53 Comment(0)
G
1
moment("145","hmm").format("HH:mm");

this would result in 01:45

Gimpel answered 1/8, 2017 at 0:50 Comment(0)
L
1

using HH:mm will convert 12 hrs format to 24hrs while using hh:mm will convert 12hrs format

moment("12:15 PM").format("HH:mm")
Lacee answered 13/11, 2019 at 3:18 Comment(0)
B
1

Stating your time as HH will give you 24h format, and hh will give 12h format.

You can also find it here in the documentation :https://momentjs.com/docs/#/parsing/string-format/

H, HH       24 hour time
h, or hh    12 hour time (use in conjunction with a or A)
Bedcover answered 7/6, 2022 at 0:32 Comment(0)
G
0

Here's how you can set the time of a Date object using a time String.

const date = new Date('1/1/21');
const pm = '2:30 PM';
const am = '11:00 AM';
const noon = '12:00 PM';
const midnight = '12:00 AM';

const mergeDateTime = (date, time) => {
  const parsedTime = moment(time, ['h:mm A']);
  const merged = moment(date).set({
    hours: parsedTime.get('hours'),
    minutes: parsedTime.get('minutes')
  });
  
  return merged.toDate();
};

const resultsList = document.querySelector('#results');

const inputs = [pm, am, noon, midnight];

inputs.forEach((input) => {
  const li = document.createElement('li');
  li.innerText = `${input} ➡ ${mergeDateTime(date, input)}`;
  resultsList.appendChild(li);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<h3>Results</h3>
<ul id=results />
Gwennie answered 15/12, 2020 at 16:39 Comment(0)
K
0

you can use dayjs

return dayjs(date as string).format('DD/MM/YYYY HH:mm')

you will have :

03/03/2021 16:36

for more information, you can refer to https://day.js.org/docs/en/display/format

Kopeisk answered 2/4, 2021 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.