TypeScript获取格式化日期
程序员文章站
2022-03-05 08:13:11
...
代码挺简单的,但是写出来却花费了不少功夫,主要还是不适应ts的类型机制。记下来备忘。
static getNowDate(): string {
const date = new Date();
let month: string | number = date.getMonth() + 1;
let strDate: string | number = date.getDate();
if (month <= 9) {
month = "0" + month;
}
if (strDate <= 9) {
strDate = "0" + strDate;
}
return date.getFullYear() + "-" + month + "-" + strDate + " "
+ date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}