当调用getMonth()和getDate()on date对象时,我们将获得single digit number。例如 :对于january,它显示1,但我需要将其显示为01。怎么做?
3 回答

撒科打诨
TA贡献1934条经验 获得超2个赞
("0" + this.getDate()).slice(-2)
日期,类似:
("0" + (this.getMonth() + 1)).slice(-2)

精慕HU
TA贡献1845条经验 获得超8个赞
本月的示例:
function getMonth(date) {
var month = date.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
您还可以Date使用以下功能扩展对象:
Date.prototype.getMonthFormatted = function() {
var month = this.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
添加回答
举报
0/150
提交
取消