moment.js 24h format

JavascriptMomentjs

Javascript Problem Overview


How do I display my time in 24h format instead of 12?

I am using moment.js.

I am pretty sure that these lines could have something to do with it.

   meridiem : function (hours, minutes, isLower) {
        if (hours > 11) {
            return isLower ? 'pm' : 'PM';
        } else {
            return isLower ? 'am' : 'AM';
        }
    },

How to change it?

Javascript Solutions


Solution 1 - Javascript

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 :

    H, HH	    24 hour time
    h, or hh	12 hour time (use in conjunction with a or A)

Solution 2 - Javascript

Try: moment({ // Options here }).format('HHmm'). That should give you the time in a 24 hour format.

Solution 3 - Javascript

moment("01:15:00 PM", "h:mm:ss A").format("HH:mm:ss")
**o/p: 13:15:00 **

It will convert 12hrs format to 24hrs format.

Solution 4 - Javascript

Solution 5 - Javascript

You can use

 moment("15", "hh").format('LT') 

to convert the time to 12 hours format like this 3:00 PM

Solution 6 - Javascript

HH used 24 hour format while hh used for 12 format

Solution 7 - Javascript

Use this to get time from 00:00:00 to 23:59:59

If your time is having date from it by using 'LT or LTS'

var now = moment('23:59:59','HHmmss').format("HH:mm:ss")

** https://jsfiddle.net/a7qLhsgz/**

Solution 8 - Javascript

//Format 24H use HH:mm
let currentDate = moment().format('YYYY-MM-DD HH:mm')
console.log(currentDate)

//example of current time with defined Time zone +1
let currentDateTm = moment().utcOffset('+0100').format('YYYY-MM-DD HH:mm')
console.log(currentDateTm)

<script src="https://momentjs.com/downloads/moment.js"></script>

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
QuestionRichard StenstiernaView Question on Stackoverflow
Solution 1 - JavascriptJørgen RView Answer on Stackoverflow
Solution 2 - JavascriptkayenView Answer on Stackoverflow
Solution 3 - Javascriptvinod intiView Answer on Stackoverflow
Solution 4 - JavascriptSuprView Answer on Stackoverflow
Solution 5 - JavascriptSohailView Answer on Stackoverflow
Solution 6 - JavascriptWapShivamView Answer on Stackoverflow
Solution 7 - JavascriptGajender SinghView Answer on Stackoverflow
Solution 8 - JavascriptTaha AzzabiView Answer on Stackoverflow