我正在使用方法来获取数据
function date() {
let str = '';
const currentTime = new Date();
const year = currentTime.getFullYear();
const month = currentTime.getMonth();
const day = currentTime.getDate();
const hours = currentTime.getHours();
let minutes = currentTime.getMinutes();
let seconds = currentTime.getSeconds();
if (month < 10) {
//month = '0' + month;
}
if (minutes < 10) {
//minutes = '0' + minutes;
}
if (seconds < 10) {
//seconds = '0' + seconds;
}
str += year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + ' ';
console.log(str);
}
作为输出我得到
2017-6-13 20:36:6
我想得到同样的东西,但喜欢
2017-06-13 20:36:06
但是,如果我尝试其中一条,我注释掉了,例如这一行
month = '0' + month;
我收到错误
Argument of type 'string' is not assignable to parameter of type 'number'.
我怎么能连接字符串和数字?
原文由 Anna F 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以像这样使用它。 角度环境.ts