gpt4 book ai didi

javascript - 如何更改日期格式并在前面添加0

转载 作者:行者123 更新时间:2023-12-03 07:43:49 25 4
gpt4 key购买 nike

我有这个代码:

JS代码:

var completeD = start.format("YYYY/MM/DD HH:mm");
var dt = new Date(completeD);
console.log(dt) //here it's display Tue Feb 09 2016 02:00:00 GMT+0200 (EET)
console.log(dt.getHours() + ":" + dt.getMinutes()); //the input value is 2:0

输入值为2:0应该是02:00

如果需要的话,如何在...前面添加0。

提前致谢!

最佳答案

简单的方法是编写一个函数来纠正字符串:

function normalize(n) {
if (n < 10) {
n = "0" + n;
}
return n;
}
var completeD = start.format("YYYY/MM/DD HH:mm");
var dt = new Date(completeD);
console.log(normalize(dt.getHours()) + ":" + normalize(dt.getMinutes()));

关于javascript - 如何更改日期格式并在前面添加0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com