js 計算月/周的第一天和最後一天程式碼
因為專案開發中遇到需要向後臺傳本週的開始和結束時間,以及上一週的起止時間,就琢磨了半天,總算寫出來一套,寫篇文章是為了方便自己記憶,也是分享給需要的人,水平有限,寫的不好請見諒:
1、getDateStr3函式是為了把時間物件轉變為yy-mm-dd的字串,方便傳值;
2、getWeekStartAndEnd函式是獲取周的起止時間,並且用getDateStr3轉換成字串放到陣列中,其中引數0代表當前周,-1代表前一週,-2代表上上週,以此類推,反過來也可以1代表下一週;
3、getMonthStartAndEnd函式是獲取月的起止時間,傳參同上
//獲取當前日期yy-mm-dd //date 為時間物件 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; } /** * 獲得相對當前周AddWeekCount個周的起止日期 * AddWeekCount為0代表當前周 為-1代表上一個周 為1代表下一個周以此類推 * **/ function getWeekStartAndEnd(AddWeekCount) { //起止日期陣列 var startStop = new Array(); //一天的毫秒數 var millisecond = 1000 * 60 * 60 * 24; //獲取當前時間 var currentDate = new Date(); //相對於當前日期AddWeekCount個周的日期 currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount)); //返回date是一週中的某一天 var week = currentDate.getDay(); //返回date是一個月中的某一天 var month = currentDate.getDate(); //減去的天數 var minusDay = week != 0 ? week - 1 : 6; //獲得當前周的第一天 var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); //獲得當前周的最後一天 var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6)); //新增至陣列 startStop.push(getDateStr3(currentWeekFirstDay)); startStop.push(getDateStr3(currentWeekLastDay)); return startStop; } /** * 獲得相對當月AddMonthCount個月的起止日期 * AddMonthCount為0 代表當月 為-1代表上一個月 為1代表下一個月 以此類推 * ***/ function getMonthStartAndEnd(AddMonthCount) { //起止日期陣列 var startStop = new Array(); //獲取當前時間 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)); //獲得當前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //獲得上一個月的第一天 var currentMonthFirstDay = new Date(currentYear,currentMonth,1); //獲得上一月的最後一天 var currentMonthLastDay = new Date(currentYear,currentMonth+1,0); //新增至陣列 startStop.push(getDateStr3(currentMonthFirstDay)); startStop.push(getDateStr3(currentMonthLastDay)); //返回 return startStop; }
獲取到每月的第一天和最後一天 需要傳入一個月份 如果忘記傳入 取當前月份
//獲取到每月的第一天和最後一天 getMonthFirstOrLaseDay:function(month){ var month=month || (new Date()).getMonth() //設定預設 如果不穿 取當前月份 var nowdays = new Date(); var year = nowdays.getFullYear(); if(month==0) { month=12; year=year-1; } if (month < 10) { month = "0" + month; } var firstDay = year+'' + month+'' + "01"; var myDate = new Date(year,month,0); var lastDay = year+'' + month+'' + myDate.getDate(); return {firstDay:firstDay,lastDay:lastDay} },
獲取到每個月有幾周,並且每週一和週日是哪天 如果不穿 預設取當年 當月
//獲取到每個月有幾周,並且每週一和週日是哪天 getAForWeeks:function (year,month) { var year=year || (new Date()).getFullYear() var month=month || (new Date()).getMonth() var d = new Date(); // what day is first day d.setFullYear(year,month-1,1); var w1 = d.getDay(); if (w1 == 0) w1 = 7; // total day of month d.setFullYear(year,0); var dd = d.getDate(); // first Monday if (w1 != 1) d1 = 7 - w1 + 2; else d1 = 1; week_count = Math.ceil((dd-d1+1)/7); var allWeek={}; for (var i = 0; i < week_count; i++) { var monday = d1+i*7; var sunday = monday + 6; var from = year+''+this.fnToDub(month)+''+this.fnToDub(monday); var to; if (sunday <= dd) { to = year+''+this.fnToDub(month)+''+this.fnToDub(sunday); } else { d.setFullYear(year,sunday); to = d.getFullYear()+''+this.fnToDub((d.getMonth()+1))+''+this.fnToDub(d.getDate()); } allWeek[(i+1)]={ from:from,to:to } } return {allWeek:allWeek,week_count:week_count} },
獲取當月的第一天和當月的最後一天其實還挺麻煩的,因為每個月天數可能不一樣。不過藉助 Date 物件則很容易實現:
建構函式
new Date();
new Date(value);
new Date(dateString);
new Date(year,month[,day[,hour[,minutes[,seconds[,milliseconds]]]]]);
各引數的含義:
value 代表自1970年1月1日00:00:00 (世界標準時間) 起經過的毫秒數。
dateString 表示日期的字串值。該字串應該能被 Date.parse() 方法識別
year 代表年份的整數值。為了避免2000年問題最好指定4位數的年份; 使用 1998,而不要用 98.
month 代表月份的整數值從0(1月)到11(12月)。
day 代表一個月中的第幾天的整數值,從1開始。
hour 代表一天中的小時數的整數值 (24小時制)。
minute 分鐘數。
second 秒數。
millisecond 表示時間的毫秒部分的整數值。
當月第一天和最後一天
可直接用年月日構造一個日期:
var date = new Date(),y = date.getFullYear(),m = date.getMonth(); var firstDay = new Date(y,m,1); var lastDay = new Date(y,m + 1,0);
或
var date = new Date(); var firstDay = new Date(date.getFullYear(),date.getMonth(),1); var lastDay = new Date(date.getFullYear(),date.getMonth() + 1,0);
指定月份的第一天和最後一天
比如2012年1月第一天和最後一天,運算時月份要減1
var y = 2012,m = 1 var firstDay = new Date(y,m - 1,0); console.log(firstDay); console.log(lastDay);
執行結果:
Sun Jan 01 2012 00:00:00 GMT+0800 (中國標準時間)
Tue Jan 31 2012 00:00:00 GMT+0800 (中國標準時間)