我已经创建了这个脚本,以dd/mm/yyyy的格式提前计算10天的日期:
var MyDate = new Date();
var MyDateString = new Date();
MyDate.setDate(MyDate.getDate()+10);
MyDateString = MyDate.getDate() + '/' + (MyDate.getMonth()+1) + '/' + MyDate.getFullYear();
我需要通过将这些规则添加到脚本中,使日期在日期和月份组件上以前导零出现。我好像不能让它工作。
if (MyDate.getMonth < 10)getMonth = '0' + getMonth;
and
if (MyDate.getDate <10)get.Date = '0' + getDate;
如果有人能告诉我在哪里插入这些脚本,我会非常感激。
现在你也可以使用String.prototype.padStart来快速简单地达到目标
String(new Date().getMonth() + 1).padStart(2, '0')
可用性可以在caniuse进行评估
var date = new Date()
var year = date.getFullYear()
var month = String(date.getMonth() + 1).padStart(2, '0')
var day = String(date.getDate()).padStart(2, '0')
console.log('%s/%s/%s', month, day, year)
检查
var date = new date ('7/4/2021')
var year = date.getFullYear()
var month = String(date.getMonth() + 1).padStart(2, '0')
var day = String(date.getDate())。padStart (2, ' 0 ')
/**
*预期产出:2021年4月7日
* /
Console.log ('%s/%s/%s',月,日,年)
旧浏览器的填充
String.prototype.padStart || Object.defineProperty(String.prototype, 'padStart', {
configurable : true,
writable : true,
value : function (targetLength, padString) {
'use strict'
/**
* String.prototype.padStart polyfill
* https://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date
*/
targetLength = targetLength | 0
padString = arguments.length > 1 ? String(padString) : ' '
if (this.length < targetLength && padString.length) {
targetLength = targetLength - this.length
while (padString.length < targetLength) {
padString += padString
}
return padString.slice(0, targetLength) + this
} else {
return this
}
}
})
您可以使用三元运算符将日期格式化为“if”语句。
例如:
var MyDate = new Date();
MyDate.setDate(MyDate.getDate()+10);
var MyDateString = (MyDate.getDate() < 10 ? '0' + MyDate.getDate() : MyDate.getDate()) + '/' + ((d.getMonth()+1) < 10 ? '0' + (d.getMonth()+1) : (d.getMonth()+1)) + '/' + MyDate.getFullYear();
So
(MyDate.getDate() < 10 ? '0' + MyDate.getDate() : MyDate.getDate())
将类似于if语句,其中如果getDate()返回的值小于10,则返回'0' +日期,如果大于10则返回日期(因为我们不需要加上前导0)。月份也是如此。
编辑:
忘记了getMonth以0开头,所以添加了+1来解释它。当然,您也可以只说d.g getmonth() < 9:,但我认为使用+1将有助于更容易理解。
试试这个:http://jsfiddle.net/xA5B7/
var MyDate = new Date();
var MyDateString;
MyDate.setDate(MyDate.getDate() + 20);
MyDateString = ('0' + MyDate.getDate()).slice(-2) + '/'
+ ('0' + (MyDate.getMonth()+1)).slice(-2) + '/'
+ MyDate.getFullYear();
编辑:
为了解释,.slice(-2)给出了字符串的最后两个字符。
所以无论如何,我们都可以在日期或月份后加上“0”,只要求最后两个,因为这两个总是我们想要的。
所以如果MyDate.getMonth()返回9,它将是:
("0" + "9") // Giving us "09"
加上。slice(-2)就得到了最后两个字符:
("0" + "9").slice(-2)
"09"
但如果MyDate.getMonth()返回10,它将是:
("0" + "10") // Giving us "010"
所以加上.slice(-2)会得到最后两个字符,或者:
("0" + "10").slice(-2)
"10"
我要做的是,创建我自己的自定义Date助手,看起来像这样:
var DateHelper = {
addDays : function(aDate, numberOfDays) {
aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
return aDate; // Return the date
},
format : function format(date) {
return [
("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes
("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes
date.getFullYear() // Get full year
].join('/'); // Glue the pieces together
}
}
// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Add 20 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), 20));
(也可以参看这把小提琴)