Display 12-hour and 24-hour time

后端 未结 5 1550
猫巷女王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:17

    Agreed with others, yes issues with that code but for time conversion part - maybe you could do something simple like this using JavaScript built-in functions :

    For 12-hr Format :

    let formattedTime = new Date().toLocaleTimeString('en-US');
    console.log(formattedTime)

    For 24-hr Format :

    let currentDateTime = new Date();
        let formattedTime = currentDateTime.getHours() + ":" + currentDateTime.getMinutes() +":" + currentDateTime.getSeconds();
        console.log(formattedTime)

提交回复
热议问题