admin管理员组

文章数量:1134240

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?

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?

Share Improve this question asked Nov 6, 2014 at 15:25 ramram 2,3434 gold badges29 silver badges39 bronze badges 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. – PeteG Commented Nov 6, 2014 at 15:30
  • It just outputs a string of time in 12 hour format. But hou can this be converted to 24 hour format of Date object? – ram Commented Nov 6, 2014 at 15:32
  • Can you please mark my anwer as accepted if it worked? – Pochen Commented Dec 3, 2014 at 13:47
Add a comment  | 

10 Answers 10

Reset to default 151

See documentation of moment js parse function

JSFiddle

var dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");

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

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"

var today = new Date();
let options = {  
    hour: "2-digit", minute: "2-digit"  
};  
console.log(today.toLocaleTimeString("en-us", options));

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.

moment("145","hmm").format("HH:mm");

this would result in 01:45

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")

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)

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 />

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

本文标签: javascriptConvert 12 hour (AMPM) string to 24 Date object using moment jsStack Overflow