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"


