How do you display JavaScript datetime in 12 hour AM/PM format?

后端 未结 27 3115
慢半拍i
慢半拍i 2020-11-22 02:36

How do you display a JavaScript datetime object in the 12 hour format (AM/PM)?

27条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 03:23

    Use Moment.js for this

    Use below codes in JavaScript when using moment.js

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

    The format() method returns the date in specific format.

    moment(new Date()).format("YYYY-MM-DD HH:mm"); // 24H clock
    moment(new Date()).format("YYYY-MM-DD hh:mm A"); // 12H clock (AM/PM)
    moment(new Date()).format("YYYY-MM-DD hh:mm a"); // 12H clock (am/pm)
    

提交回复
热议问题