你好,我有这个使用 javascript :
var newhour = 20.5;
hour.setHours(newhour);
问题是当我执行 console.log(hour)
时,我得到 20:05
但我想得到 20:30
.
我该怎么做?
非常感谢!
最佳答案
需要将0.5
转换成分钟数:
var newhour = 20.5;
var hour = new Date();
var newhours = Math.floor(newhour),
newmins = 60 * (newhour - newhours);
hour.setHours(newhours);
hour.setMinutes(newmins);
console.log(hour.toTimeString());
关于javascript - 将数字转换为小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727397/