获取当前时间:
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
alert(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
获取一星期前的时间:var now = new Date();
var date = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
alert(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second); 获取前一个月时间:
function getPreMonth(date) {
var arr = date.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = arr[2]; //获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); //获取当前日期中月的天数
var year2 = year;
var month2 = parseInt(month) - 1;
if (month2 == 0) {//如果是1月份,则取上一年的12月份
year2 = parseInt(year2) - 1;
month2 = 12;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {//如果原来日期大于上一月的日期,则取当月的最大日期。比如3月的30日,在2月中没有30
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;//月份填补成2位。
}
var t2 = year2 + '-' + month2 + '-' + day2;
return t2;
}
// 调用
alert(getPreMonth("2014-01-25"));
获取前三个月的时间
function get3MonthBefor(){
//获取三个月前的时间
var resultDate,year,month,date,hms;
var currDate = new Date();
year = currDate.getFullYear();
month = currDate.getMonth()+1;
date = currDate.getDate();
hms = currDate.getHours() + ':' + currDate.getMinutes() + ':' + (currDate.getSeconds() < 10 ? '0'+currDate.getSeconds() : currDate.getSeconds());
switch(month)
{
case 1:
case 2:
case 3:
month += 9;
year--;
break;
default:
month -= 3;
break;
}
month = (month < 10) ? ('0' + month) : month;
resultDate = year + '-'+month+'-'+date+' ' + hms;
return resultDate;
}