js 獲取本周、上周、本月、上月、本季度、上季度的開始結(jié)束日期
js 獲取 本周、上周、本月、上月、本季度、上季度的開始結(jié)束日期
/** * 獲取本周、本季度、本月、上月的開始日期、結(jié)束日期 */ var now = new Date(); //當(dāng)前日期 var nowDayOfWeek = now.getDay(); //今天本周的第幾天 var nowDay = now.getDate(); //當(dāng)前日 var nowMonth = now.getMonth(); //當(dāng)前月 var nowYear = now.getYear(); //當(dāng)前年 nowYear += (nowYear < 2000) ? 1900 : 0; // 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 < 10) { mymonth = "0" + mymonth; } if (myweekday < 10) { myweekday = "0" + myweekday; } return (myyear + "-" + mymonth + "-" + myweekday); } //獲得某月的天數(shù) 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 (nowMonth < 3) { quarterStartMonth = 0; } if (2 < nowMonth && nowMonth < 6) { quarterStartMonth = 3; } if (5 < nowMonth && nowMonth < 9) { quarterStartMonth = 6; } if (nowMonth > 8) { quarterStartMonth = 9; } return quarterStartMonth; } //獲得本周的開始日期 function getWeekStartDate() { var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); return formatDate(weekStartDate); } //獲得本周的結(jié)束日期 function getWeekEndDate() { var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)); return formatDate(weekEndDate); } //獲得上周的開始日期 function getLastWeekStartDate() { var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7); return formatDate(weekStartDate); } //獲得上周的結(jié)束日期 function getLastWeekEndDate() { var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1); return formatDate(weekEndDate); } //獲得本月的開始日期 function getMonthStartDate() { var monthStartDate = new Date(nowYear, nowMonth, 1); return formatDate(monthStartDate); } //獲得本月的結(jié)束日期 function getMonthEndDate() { var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth)); return formatDate(monthEndDate); } //獲得上月開始時(shí)間 function getLastMonthStartDate() { var lastMonthStartDate = new Date(nowYear, lastMonth, 1); return formatDate(lastMonthStartDate); } //獲得上月結(jié)束時(shí)間 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); } //或的本季度的結(jié)束日期 function getQuarterEndDate() { var quarterEndMonth = getQuarterStartMonth() + 2; var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); return formatDate(quarterStartDate); }
js 計(jì)算月/周的第一天和最后一天
因?yàn)轫?xiàng)目開發(fā)中遇到需要向后臺(tái)傳本周的開始和結(jié)束時(shí)間,以及上一周的起止時(shí)間,就琢磨了半天,總算寫出來一套,寫篇文章是為了方便自己記憶,也是分享給需要的人,水平有限,寫的不好請(qǐng)見諒:
getDateStr3函數(shù)是為了把時(shí)間對(duì)象轉(zhuǎn)變?yōu)閥y-mm-dd的字符串,方便傳值;
getWeekStartAndEnd函數(shù)是獲取周的起止時(shí)間,并且用getDateStr3轉(zhuǎn)換成字符串放到數(shù)組中,其中參數(shù)0代表當(dāng)前周,-1代表前一周,-2代表上上周,以此類推,反過來也可以1代表下一周;
getMonthStartAndEnd函數(shù)是獲取月的起止時(shí)間,傳參同上
//獲取當(dāng)前日期yy-mm-dd //date 為時(shí)間對(duì)象 function getDateStr3(date) { var year = ""; var month = ""; var day = ""; var now = date; year = ""+now.getFullYear(); if((now.getMonth()+1)<10){ month = "0"+(now.getMonth()+1); }else{ month = ""+(now.getMonth()+1); } if((now.getDate())<10){ day = "0"+(now.getDate()); }else{ day = ""+(now.getDate()); } return year+"-"+month+"-"+day; } /** * 獲得相對(duì)當(dāng)前周AddWeekCount個(gè)周的起止日期 * AddWeekCount為0代表當(dāng)前周 為-1代表上一個(gè)周 為1代表下一個(gè)周以此類推 * **/ function getWeekStartAndEnd(AddWeekCount) { //起止日期數(shù)組 var startStop = new Array(); //一天的毫秒數(shù) var millisecond = 1000 * 60 * 60 * 24; //獲取當(dāng)前時(shí)間 var currentDate = new Date(); //相對(duì)于當(dāng)前日期AddWeekCount個(gè)周的日期 currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount)); //返回date是一周中的某一天 var week = currentDate.getDay(); //返回date是一個(gè)月中的某一天 var month = currentDate.getDate(); //減去的天數(shù) var minusDay = week != 0 ? week - 1 : 6; //獲得當(dāng)前周的第一天 var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); //獲得當(dāng)前周的最后一天 var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6)); //添加至數(shù)組 startStop.push(getDateStr3(currentWeekFirstDay)); startStop.push(getDateStr3(currentWeekLastDay)); return startStop; } /** * 獲得相對(duì)當(dāng)月AddMonthCount個(gè)月的起止日期 * AddMonthCount為0 代表當(dāng)月 為-1代表上一個(gè)月 為1代表下一個(gè)月 以此類推 * ***/ function getMonthStartAndEnd(AddMonthCount) { //起止日期數(shù)組 var startStop = new Array(); //獲取當(dāng)前時(shí)間 var currentDate = new Date(); var month=currentDate.getMonth()+AddMonthCount; if(month<0){ var n = parseInt((-month)/12); month += n*12; currentDate.setFullYear(currentDate.getFullYear()-n); } currentDate = new Date(currentDate.setMonth(month)); //獲得當(dāng)前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當(dāng)前年份4位年 var currentYear = currentDate.getFullYear(); //獲得上一個(gè)月的第一天 var currentMonthFirstDay = new Date(currentYear, currentMonth,1); //獲得上一月的最后一天 var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0); //添加至數(shù)組 startStop.push(getDateStr3(currentMonthFirstDay)); startStop.push(getDateStr3(currentMonthLastDay)); //返回 return startStop; }
好了這篇文章就介紹到這了
相關(guān)文章
JavaScript中實(shí)現(xiàn)數(shù)組分組功能的方法詳解
最近,JavaScript引入了一個(gè)備受期待的功能:原生支持?jǐn)?shù)組分組,這一特性使得在處理復(fù)雜的數(shù)據(jù)集時(shí)變得更加簡(jiǎn)單和高效,本文將深入探討這一全新的JavaScript特性,希望對(duì)大家有所幫助2023-12-12layui框架中l(wèi)ayer父子頁(yè)面交互的方法分析
這篇文章主要介紹了layui框架中l(wèi)ayer父子頁(yè)面交互的方法,結(jié)合實(shí)例形式分析了layer父子頁(yè)面交互的常用技巧以及l(fā)ayer彈出多個(gè)iframe找到父頁(yè)面的操作方法,需要的朋友可以參考下2017-11-11

typescript使用 ?. ?? ??= 運(yùn)算符的方法步驟

淺談js中用$(#ID)來作為選擇器的問題(id重復(fù)的時(shí)候)

JavaScript this關(guān)鍵字指向常用情況解析

js自己實(shí)現(xiàn)一個(gè)大文件切片上傳+斷點(diǎn)續(xù)傳的示例代碼

Javascript查看大圖功能代碼實(shí)現(xiàn)