moment.js formato 24h


134

Come posso visualizzare l'ora in formato 24 ore anziché 12?

Sto usando moment.js.

Sono abbastanza sicuro che queste linee potrebbero avere qualcosa a che fare con esso.

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

Come cambiarlo?

Risposte:


378

Indicare il tuo tempo come HHti darà il formato 24h e hhdarà il formato 12h.

Puoi trovarlo anche qui nella documentazione :

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

9
Stackoverflow è sempre più corto di un DRTL di RTFM ;-) grazie per avermi fatto trovare rapidamente questa risposta!
Pipo,

29

Prova: moment({ // Options here }).format('HHmm'). Questo dovrebbe darti l'ora in un formato di 24 ore.


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

fornirà il formato convertito 24 ore nel formato 12 ore.


In realtà converte il formato 12 ore nel formato 24 ore. E funziona per me.
NomanJaved


8

Puoi usare

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

per convertire l'ora in formato 12 ore in questo modo 3:00 PM


2

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



0

HHusato il formato 24 ore mentre hhusato per il formato 12

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.