关于js获取前几周,几个月的js小脚本
程序员文章站
2022-05-14 18:16:19
...
最近在做项目的时候,遇到了获取上几周,上几个月的的开始时间,和结束时间,在网上搜罗了一遍并没有完全满足自己需求的,就根据网上的例子,简单修改了一下,供大家参考:
var getWeek=function(mark){
var week = "日一二三四五六".split("")[new Date().getDay()];
var endTime ;
if(week == "一"){
endTime = addDate(new Date(),-1)
} else if(week == "二"){
endTime = addDate(new Date(),-2)
} else if(week == "三"){
endTime = addDate(new Date(),-3)
} else if(week == "四"){
endTime = addDate(new Date(),-4)
} else if(week == "五"){
endTime = addDate(new Date(),-5)
} else if(week == "六"){
endTime = addDate(new Date(),-6)
} else if(week == "日") {
endTime = addDate(new Date(), -7)
}
var endTimeStr = formatDate(endTime);
addDate(endTime,-7*mark+1)
var startTime = formatDate(endTime);
return startTime+"~"+endTimeStr;
}
var addDate= function(date,n){
date.setDate(date.getDate()+n);
return date;
};
下面是格式化代码:
var formatDate = function(date){
var year = date.getFullYear()+'-';
var month = "";
if(date.getMonth()+1<10){
month = "0"+(date.getMonth()+1)+'-';
}else{
month = (date.getMonth()+1)+'-';
}
var day = "";
if(date.getDate()<10){
day = "0"+date.getDate();
}else{
day = date.getDate();
}
return year+month+day;
};
上一篇: hdu2086A1 = ?解题报告---推导求通项
下一篇: py爬虫