Skip to content
Advertisement

Moment JS format() not displaying correct time [closed]

This result is wrong:

console.log(moment("2020-08-07 09:10:48", "yyyy-MM-DD HH:mm:ss")
        .format("ddd, DD.MM.yyyy HH:mm:ss"))

-> Fri, 07.08.2020 00:00:00


These return the correct values

console.log(moment("2020-08-07 09:10:48").hour())

-> 9

console.log(moment("2020-08-07 09:10:48").format())

-> 2020-08-07T09:10:48+02:00

Apparently the date was correctly parsed but format does not work as expected?

Advertisement

Answer

Your are passing yyyy which is wrong. Expected YYYY

Try this.

console.log(moment("2020-08-07 09:10:48", "YYYY-MM-DD HH:mm:ss").format("ddd, DD.MM.YYYY HH:mm:ss"))

console.log(moment("2020-08-07 09:10:48").hour())

console.log(moment("2020-08-07 09:10:48").format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement