gpt4 book ai didi

JavaScript 前导零

转载 作者:行者123 更新时间:2023-12-03 05:06:58 24 4
gpt4 key购买 nike

我正在尝试使用 getHours 和 getMinutes 在稍后的函数中使用它们。问题是我总是希望最终的数字是3或4位数字和2位数字。发生的情况是,当分钟为 0-9 时,1:04 的结果为 14。这是我的代码,它不能解决问题。

    $hours = (new Date).getHours(),
$mins = (new Date).getMinutes();
function addZero($hours) {
if ($hours < 10) {
$hours = "0" + $hours;
}
return $hours;
}
function addZero($mins) {
if ($mins < 10) {
$mins = "0" + $mins;
}
return $mins;
}
$nowTimeS = $hours + "" + $mins;


// Convert string with now time to int
$nowTimeInt = $nowTimeS;

最佳答案

问题是您有两个同名的函数,但您从未调用该函数:

$date = new Date();
$hours = $date.getHours(),
$mins = $date.getMinutes();

$nowTimeS = addZero($hours) + "" + addZero($mins);

// Convert string with now time to int
$nowTimeInt = $nowTimeS;


function addZero($time) {
if ($time < 10) {
$time = "0" + $time;
}

return $time;
}

关于JavaScript 前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41974131/

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