function getWeek() {
//getWeekType()这个方法如果不传参数 或者传入0,返回的是本周的日期范围
//如果要下周的范围则传入1,上周的传入-1;
//注:这个是从周一开始算一周的开始,周日为结束。
console.log('this', this)
this.nowTime = new Date();
this.init = function() {
this.dayInWeek = this.nowTime.getDay();
this.dayInWeek == 0 && (this.dayInWeek = 7);
this.thsiWeekFirstDay = this.nowTime.getTime() - (this.dayInWeek - 1) * 86400000;
this.thisWeekLastDay = this.nowTime.getTime() + (7 - this.dayInWeek) * 86400000;
return this;
};
this.getWeekType = function(type) {
type = ~~type;
var firstDay = this.thsiWeekFirstDay + type * 7 * 86400000;
var lastDay = this.thisWeekLastDay + type * 7 * 86400000;
return this.getWeekHtml(firstDay, lastDay);
}
this.formateDate = function(time) {
var newTime = new Date(time)
var year = newTime.getFullYear();
var month = newTime.getMonth() + 1;
var day = newTime.getDate();
return year + "-" + (month >= 10 ? month : "0" + month) + "-" + (day >= 10 ? day : "0" + day);
};
this.getWeekHtml = function(f, l) {
return this.formateDate(f) + "," + this.formateDate(l);
};
}
``
调用
function contrastTime(){
console.log('this',this)
//此处this 将指向window
var getWeek = new this.getWeek();
var week = getWeek.init().getWeekType(1).split(",");
var startWeekTime = new Date(week[0]).getTime();
var endWeekTime = new Date(week[1]).getTime();
console.log('startWeekTime', startWeekTime)
console.log('endWeekTime', endWeekTime)
}
调用拿到下周第一天的时间戳跟最后一天的时间戳
window.onload = function(){
contrastTime()
}
获取上/下/当前周的第一天和最后一天的时间戳
最新推荐文章于 2023-03-31 17:46:48 发布