直接上代码,其实就是围绕毫秒数做一些运算。
演示见:这里。
function WeekDate() {
/**
* 基准时间,所有计算以此为基础
*/
var _calcDate = new Date();
/**
* 一天的豪秒数
*/
var _day = 1000 * 60 * 60 * 24;
this.getThisWeekDate = getThisWeekDate;
this.getPrevWeekDate = getPrevWeekDate;
this.getNextWeekDate = getNextWeekDate;
this.wrapDate = wrapDate;
this.getDayMillisecond = getDayMillisecond;
/**
* 取上周开始至上周结束日期
*
* @return Array [0]上周第一天 [1]上周最后一天
*/
function getPrevWeekDate() {
// 取上周结束日期
var lastDay = new Date(_calcDate - (_calcDate.getDay()) * _day);
// 取上周开始日期
var firstDay = new Date((lastDay * 1) - 6 * _day);
// 更新基准时间
_calcDate = firstDay;
return [wrapDate(firstDay), wrapDate(lastDay)];
}
/**
* 取下周开始至下周结束日期
*
* @return Array [0]上周第一天 [1]上周最后一天
*/
function getNextWeekDate() {
// 取下周开始日期
var firstDay = new Date((_calcDate * 1) + (6 - _calcDate.getDay() + 2) * _day);
// 取下周结束日期
var lastDay = new Date((firstDay * 1) + 6 * _day);
// 更新基准时间
_calcDate = firstDay;
return [wrapDate(firstDay), wrapDate(lastDay)];
}
/**
* 取本周开始至本周结束日期
*
* @return Array [0]本周第一天 [1]本周最后一天
*/
function getThisWeekDate() {
_calcDate = new Date();
// 第一天日期
var firstDay = new Date(_calcDate - (_calcDate.getDay() - 1) * _day);
// 最后一天日期
var lastDay = new Date((firstDay * 1) + 6 * _day);
return [wrapDate(firstDay), wrapDate(lastDay)];
}
function wrapDate($date) {
var m = $date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = $date.getDate();
d = d < 10 ? "0" + d : d;
return $date.getFullYear() + "-" + m + "-" + d;
}
function getDayMillisecond() {
return _day;
}
}
//var weekDate = new WeekDate();
//alert(weekDate.getNextWeekDate());
//alert(weekDate.getNextWeekDate());
//alert(weekDate.getThisWeekDate());
//alert(weekDate.getPrevWeekDate());
//alert(weekDate.getPrevWeekDate());
目前没有留言,等您坐沙发呢!