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

JavascriptMomentjs

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

See documentation of moment js parse function

JSFiddle

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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"

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

this would result in 01:45

Solution 7 - Javascript

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

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionramView Question on Stackoverflow
Solution 1 - JavascriptPochenView Answer on Stackoverflow
Solution 2 - JavascriptHakan FıstıkView Answer on Stackoverflow
Solution 3 - JavascriptNeel RathodView Answer on Stackoverflow
Solution 4 - JavascriptPriyanka ShirsathView Answer on Stackoverflow
Solution 5 - JavascriptRavindra VairagiView Answer on Stackoverflow
Solution 6 - JavascriptPrashanti.DView Answer on Stackoverflow
Solution 7 - JavascriptJong Yun KimView Answer on Stackoverflow
Solution 8 - JavascriptRaouf MakhloufView Answer on Stackoverflow
Solution 9 - JavascriptJBallinView Answer on Stackoverflow