首頁 >web前端 >js教程 >js取得時間(本週、本季、本月..)_javascript技巧

js取得時間(本週、本季、本月..)_javascript技巧

WBOY
WBOY原創
2016-05-16 17:12:521219瀏覽

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);
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn