您好,欢迎访问一九零五行业门户网

js获取时间(本周、本季度、本月..)_javascript技巧

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 = 0 + mymonth;
}
if(myweekday myweekday = 0 + myweekday;
}
return (myyear+-+mymonth + - + myweekday);
}
//获得某月的天数
function getmonthdays(mymonth){
var monthstartdate = new date(nowyear, mymonth, 1);
var monthenddate = new date(nowyear, mymonth + 1, 1);
var days = (monthenddate - monthstartdate)/(1000 * 60 * 60 * 24);
return days;
}
//获得本季度的开端月份
function getquarterstartmonth(){
var quarterstartmonth = 0;
if(nowmonthquarterstartmonth = 0;
}
if(2quarterstartmonth = 3;
}
if(5quarterstartmonth = 6;
}
if(nowmonth>8){
quarterstartmonth = 9;
}
return quarterstartmonth;
}
//获得本周的开端日期
function getweekstartdate() {
var weekstartdate = new date(nowyear, nowmonth, nowday - nowdayofweek);
return formatdate(weekstartdate);
}
//获得本周的停止日期
function getweekenddate() {
var weekenddate = new date(nowyear, nowmonth, nowday + (6 - nowdayofweek));
return formatdate(weekenddate);
}
//获得本月的开端日期
function getmonthstartdate(){
var monthstartdate = new date(nowyear, nowmonth, 1);
return formatdate(monthstartdate);
}
//获得本月的停止日期
function getmonthenddate(){
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(quarterstartdate);
}
//或的本季度的停止日期
function getquarterenddate(){
var quarterendmonth = getquarterstartmonth() + 2;
var quarterstartdate = new date(nowyear, quarterendmonth, getmonthdays(quarterendmonth));
return formatdate(quarterstartdate);
}
其它类似信息

推荐信息