javascript - 在新的 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()
解决方案
因为getMinutes
返回一个数字,而不是(格式化的)字符串。您必须自己将数字格式化为字符串。例如:
const lead0 = n => String(n).padStart(2, "0");
console.log(lead0(10)) // "10"
console.log(lead0(1)) // "01"
推荐阅读
- java - 来自 PostgreSQL 的流行(具有获取大小)
- matrix - 答案集编程:重新排列矩阵,使第 2 行中没有相同的数字顺序
- html - 如何从 fontawesome 5 svg 开始评分?
- android - 隐藏Toast后是否可以完成测试?
- angular - 如何在组件之间共享来自 http 响应的数据?
- sql - Query to find rows with nearest date in future
- html - parttails html文件更改未显示在angularjs应用程序中
- d3.js - 使用 fitSize 函数无法正确显示特征
- javascript - 单击链接属性时强制在加载前重新加载页面
- hibernate - 如何在hibernate和jpa中加入uuid列?