js 根据n年第n周,获取此周的开始和结束日期;根据年季度获取开始日期结束日期
程序员文章站
2022-06-07 11:51:33
...
1、根据年周获取周的开始日期和结束日期
// 第n年的第n周,获取开始和结束时间 (星期一为一周的开始),例如2020-1
function week_date(weekstr){
let year = weekstr.split("-")[0]
let weekNo = weekstr.split("-")[1]
// 此年1号是星期几
let oneday = new Date(year+'-01-01').getDay() //0-6
// 方便计算,当为星期天时为7
if(oneday==0){
oneday = 7
}
let one_fistday;
let one_lastday;
// 如果1号刚好是星期一
if(oneday == 1){
one_fistday = year+'-01-01'
one_lastday = year+'-01-07'
}else{
let jj = 8-oneday
one_fistday = (year-1)+'-12-'+(31-oneday+2>9?31-oneday+2:'0'+(31-oneday+2))
one_lastday = year+'-01-'+(jj>9?jj:'0'+jj)
}
let fistday;
let lastday;
// 如果刚好是第一周
if(weekNo ==1){
fistday = one_fistday
lastday = one_lastday
}else{
fistday = addDate(one_lastday,(weekNo-2)*7+1)
lastday = addDate(one_lastday,(weekNo-1)*7)
}
return [fistday,lastday]
}
//日期加减法 date参数为计算开始的日期,days为需要加的天数
//格式:addDate('2017-1-11',20)
function addDate(date,days){
var d=new Date(date);
d.setDate(d.getDate()+days);
var m=d.getMonth()+1;
return d.getFullYear()+'-'+(m>9?m:'0'+m)+'-'+(d.getDate()>9?d.getDate():'0'+d.getDate());
}
2、根据年季度获取开始日期和结束日期
//年几季度开始日期
var qs = new Date(年, (第几季度 - 1) * 3, 1);
//年几季度结束日期
var qe = new Date(年, 第几季度 * 3, 0);
var startM=qs.getMonth()+1;
var endM=qe.getMonth()+1;
alert(qs.getFullYear()+'-'+(startM<10?'0'+startM:startM)+'-'+(qs.getDate()<10?'0'+qs.getDate():qs.getDate()));
alert(qe.getFullYear()+'-'+(endM<10?'0'+endM:endM)+'-'+(qe.getDate()<10?'0'+qe.getDate():qe.getDate()));