use*_*233 4 javascript jquery date datepicker
所以我写了一个方法,它取一个数字并减去当前日期的月数.
我试图找出如何在少于10的月份之前添加"0".另外,如何在小于10的日子前面添加"0".
目前,它返回对象时(2012-6-9).它返回6和9,前面没有'0',有人可以告诉我该怎么做吗?
这是我的代码
lastNmonths = function(n) {
var date = new Date();
if (n <= 0)
return [date.getFullYear(), date.getMonth() + 1 , date.getDate()].join('-');
var years = Math.floor(n/12);
var months = n % 12;
if (years > 0)
date.setFullYear(date.getFullYear() - years);
if (months > 0) {
if (months >= date.getMonth()) {
date.setFullYear(date.getFullYear()-1 );
months = 12 - months;
date.setMonth(date.getMonth() + months );
} else {
date.setMonth(date.getMonth() - months);
}
}
}
return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-');
};
Run Code Online (Sandbox Code Playgroud)
Arn*_*ist 14
您还可以通过使用以下方法避免测试n <10
("0" + (yourDate.getMonth() + 1)).slice(-2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10212 次 |
| 最近记录: |