Leading zeros in minutes

后端 未结 5 1894
眼角桃花
眼角桃花 2020-12-31 17:20

I created a clock to be placed in the header of my website. The time is not displaying a zero for minutes < 10. For example if the time is 10:50, it will only show 10:5 ,

5条回答
  •  被撕碎了的回忆
    2020-12-31 18:01

    I like this way of doing things... Javascript add leading zeroes to date

    const d = new Date();
    const date = (`0${d.getMinutes()}`).slice(-2);
    console.log(date); // 09;
    

    2019 Update: But I now prefer

    const d = new Date();
    const date = String(d.getMinutes()).padStart(2, '0');
    console.log(date); // 09;
    

提交回复
热议问题