首页 > 解决方案 > 在新的 Date () 方法中显示分钟

问题描述

在新的 Date() 方法中,最多 10 分钟显示不带 0(例如,17:05 显示为 17:5;18:09 显示为 18:9,...)。为什么分分钟不显示0?

new Date().getHours() +":"+ new Date().getMinutes() +"; "+new Date().getDate() + " "+ (new Date().toLocaleString("en-us ", { 月: "long" })) + " "+new Date().getFullYear()

标签: javascripttime

解决方案


因为getMinutes返回一个数字,而不是(格式化的)字符串。您必须自己将数字格式化为字符串。例如:

const lead0 = n => String(n).padStart(2, "0");

console.log(lead0(10)) // "10"
console.log(lead0(1)) // "01"

推荐阅读