使用Date对象可以获取时间相关的信息。
获取当前时间:
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);