var d = new Date();
var year = d.getFullYear();
var month = d.getMonth()+1;
var day = d.getDate();
//算出前180天的时间戳
var o = new Date(d.getTime()-180*24*60*60*1000);
var oyear = o.getFullYear();
var omonth = o.getMonth()+1;
var oday = o.getDate();
var date = []
setTimeout(function(){
//判断半年前是否今年
//不是今年
if (year!=oyear) {
monthFn(oyear,omonth,oday)
for (var m = omonth+1; m <= 12; m++) {
monthFn(oyear,m,1)
}
for (var m = 1; m <=month-1; m++) {
monthFn(year,m,1)
}
for (var i = 1; i <= day; i++) {
date.push(year +"/"+ month +"/"+ i)
}
}
//是今年
else{
monthFn(oyear,momonth,oday)
for (var m = omonth-1; m <= month-1; m++) {
monthFn(oyear,m,1)
}
for (var i = 1; i <= day; i++) {
date.push(year +"/"+ month +"/"+ i)
}
}
console.log(date)
},100)
//判断是否闰年
function isLeapYear(){
return (0==this%4&&((this%100!=0)||(this%400==0)));
}
//判断月份
function monthFn(y,m,d){
//2月份
if(m == 2){
if (isLeapYear(y)) {
for (var i = d; i <= 29; i++) {
date.push(y +"/"+ m +"/"+ i)
}
}
else{
for (var i = d; i <= 28; i++) {
date.push(y +"/"+ m +"/"+ i)
}
}
}
//31天月
if(m == 1|| m == 3||m == 5||m == 7||m == 8||m == 10||m == 12){
for (var i = d; i <= 31; i++) {
date.push(y +"/"+ m +"/"+ i)
}
}
//30天月
if(m == 4||m == 6||m == 9||m == 11){
for (var i = d; i <= 30; i++) {
date.push(y +"/"+ m +"/"+ i)
}
}
}
jq创建当前时间的前半年日期
最新推荐文章于 2024-09-02 13:38:28 发布