三國(guó)紛爭(zhēng)
2020-02-04 15:00:42
當(dāng)調(diào)用getMonth()和getDate()on date對(duì)象時(shí),我們將獲得single digit number。例如 :對(duì)于january,它顯示1,但我需要將其顯示為01。怎么做?
3 回答

撒科打諢
TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
("0" + this.getDate()).slice(-2)
日期,類似:
("0" + (this.getMonth() + 1)).slice(-2)

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
本月的示例:
function getMonth(date) {
var month = date.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
您還可以Date使用以下功能擴(kuò)展對(duì)象:
Date.prototype.getMonthFormatted = function() {
var month = this.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
添加回答
舉報(bào)
0/150
提交
取消