Display 12-hour and 24-hour time

后端 未结 5 1565
猫巷女王i
猫巷女王i 2021-02-14 10:30

I want to make a webpage that displays the current time. When the \"12-hour format\" button is clicked, the time in 12-hour time will display in the div area. When the \"24-hour

5条回答
  •  时光取名叫无心
    2021-02-14 11:23

    Why dont you just use a library like Moment.js to do this for you.

    http://momentjs.com/docs/

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

    so just use this code in JavaScript when using moment.js

    the moment() method returns the current date in your specific format. So when you the user clicks the button you can call the following method on each button

    moment().format("YYYY-MM-DD HH:mm"); // 24H clock
    moment().format("YYYY-MM-DD hh:mm"); // 12H clock
    

    Havn't tested this , but it should work

提交回复
热议问题