【发布时间】:2020-01-21 10:18:53
【问题描述】:
我有一个可以格式化的日期和时间。它大部分时间都有效,但如果时间在倒数第二个空格中有“0”,它将返回错误。例如,时间“10.30”将被正确格式化,但“10.03”将返回不带零的“10.3”。
我的代码:
const today: Date = new Date();
const date: Date = new Date(item.receivedDateTime);
let time: string;
if (date.getFullYear() === today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate()) {
time = date.getHours() + ":" + date.getMinutes();
} else {
time = date.getDate() + "/" + (date.getMonth() + 1);
}
以上将错误地格式化时间。代码有什么问题?我不喜欢使用 padstart,因为 IE11 或 moment.js 不支持它。
【问题讨论】:
-
我认为你必须使用 ldap 函数在小值到 9 之前添加 '0'
-
我给你做了一个sn-p。请添加ìtem`的示例
-
“代码有什么问题?” - 好吧,技术上没什么。你只是没有让它做你真正想要的。
getMinutes返回一个整数,所以你不能指望一个前导零会神奇地突然出现。所以如果你想要一个,你必须确保它被添加到输出中。
标签: javascript reactjs typescript