周六当每周的第一天
<el-date-picker
v-model="week_value"
type="week"
{{-- format="yyyy 第 WW 周"--}}
:format="'yyyy 第' + cur_week + '周'"
@input="Dayofweek"
value-format="yyyy-MM-dd"
placeholder="选择周"
:picker-options="{ firstDayOfWeek: 6 }"
style="width: 150px"
@change="getData">
</el-date-picker>
week_value: '',
cur_week: null,
// 标准日转化为年月日
transformTime(date){
var dd = new Date(date);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
m = m < 9 ? "0" + m : m;
d = d < 9 ? "0" + d : d;
return y + "-" + m + "-" + d;
},
//字符串转日期格式,strDate要转为日期格式的字符串
setDate(strDate){
var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
return date;
} ,
//获取指定日期所在周的周一的日期
GetMonday(dd) {
var date = new Date(dd);
// var week = dd.getDay(); //获取时间的星期数
var week = new Date(date).getDay(); //获取时间的星期数
var minus = week ? week - 1 : 6;
date.setDate(date.getDate() - minus); //获取minus天前的日期
var y = date.getFullYear();
var m = date.getMonth() + 1; //获取月份
var d = date.getDate();
return y + "-" + m + "-" + d;
},
//获取指定日期所在周的上周六的日期
GetSaturday(dd) {
//获取时间段
var date = new Date(dd);
//获取日期字符串对应的时间戳
var timestamp = new Date(date).getTime();
//获取星期几
var currentDay = new Date(date).getDay();
//获取时间戳
var previousSaturdayTime;
if (currentDay == 0) {
//获取该天前一个星期六的时间戳
previousSaturdayTime = timestamp- 24 * 60 * 60 * 1000;
} else {
//获取该天前一个星期六的时间戳
previousSaturdayTime = timestamp - (currentDay + 1) * 24 * 60 * 60 * 1000;
}
var saturdayTime = new Date(previousSaturdayTime);
let saturdayYear = saturdayTime.getFullYear();
let saturdayMonth = saturdayTime.getMonth() + 1;
let saturdayDay = saturdayTime.getDate();
saturdayMonth = saturdayMonth >= 10 ? saturdayMonth : '0' + saturdayMonth;
saturdayDay = saturdayDay >= 10 ? saturdayDay : '0' + saturdayDay;
let lastSatDay = saturdayYear+'-'+saturdayMonth+'-'+saturdayDay;
return lastSatDay
},
Dayofweek(){
let Saturday = this.GetSaturday(this.week_value); //周六日期
let curweek = this.getWeek(Saturday) ;
this.cur_week = curweek;
},
getWeek(dt) {
let d1 = new Date(dt);
let d2 = new Date(dt);
d2.setMonth(0);
d2.setDate(1);//3 周一为本周第一天;2 周日为本周第一天;1 周六为本周第一天
let rq = d1 - d2;
let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
let num = Math.ceil(days / 7);
return num ;
},
getData(){
var that = this
var date = that.week_value;
var dd = that.setDate(date);
var date_begin = that.GetSaturday(dd);
var d_end = that.setDate(date_begin);
d_end = new Date((d_end/1000+(86400*6))*1000); //日期加6天
var date_end = that.transformTime(d_end);
}
created() {
this.selectType = this.selectList[0].id;
var date = new Date();
this.week_value = this.GetMonday(date);
this.Dayofweek();
this.getData();
},
//日期加天数的方法
//dataStr日期字符串
//dayCount 要增加的天数
//return 增加n天后的日期字符串
function dateAddDays(dataStr,dayCount) {
var strdate=dataStr; //日期字符串
var isdate = new Date(strdate.replace(/-/g,"/")); //把日期字符串转换成日期格式
isdate = new Date((isdate/1000+(86400*dayCount))*1000); //日期加1天
var pdate = isdate.getFullYear()+"-"+(isdate.getMonth()+1)+"-"+(isdate.getDate()); //把日期格式转换成字符串
return pdate;
}