Js程式碼
/**
* 取得本週、本季、本月、上月的開端日期、停止日期
** >var now = new Date(); //當前日期
var nowDayOfWeek = now.getDay(); //今天本週的第幾天
var nowDay = now.getDate(); //當前日
var nowMonth = now.getMonth(); //當月
var nowYear = now.getYear(); //當年度
nowYear = (nowYear
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
//格局化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() 1;
var myweekday = date.getDate();
if(mymonth mymonth = ">mymonth = " 0" mymonth;
}
if(myweekday myweekday = "0" myweekday;
}
return (myyear "-" mymonth "-" myweekday)
return (myyear "-" mymonth "-" myweekday)
}
//取得某月的天數
function getMonthDays(myMonth){
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthnowDate = new Date Dated , myMonth 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}
return days;
}
return days;
}
取得本季度的開端月份
function getQuarterStartMonth(){
var quarterStartMonth = 0;
if(nowMonthquarterStartMonth = 0;
} quarterStartMonth = 0;
} quarterStartMonth = 3;
}
if(5quarterStartMonth = 6;
}
if(nowMonth>8){
quarterStartMonth = 9;
}
return quarterStartMonth;
}
//取得本週的開端日期
function getWeekStartDate() { return formatDate(weekStartDate);
}
//獲得本週的停止日期
function getWeekEndDate( weekEndDate = new Date(nowYear, nowMonth, nowDay (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}
//取得本月的開端日期
}
//取得本月的開端日期var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//取得本月的停止日(){
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}
//拿上月開端>/function getLastMonthStartDate(){
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
function getLastMonthEndDate(){
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
得到本季度的季度開端日期
function getQuarterStartDate(){
var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartMonth(), 1);
return formatDate(quarterStartDate); //或的本季度的停止日期
function getQuarterEndDate(){
var quarterEndMonth = getQuarterStartMonth() 2;
var quarterStartDate = new Date(nowYear, quarterEndth, quarterStartDate = new Date(nowYear, quarterEndth, Monarter); return formatDate(quarterStartDate);
}