js 計算月 周的第一天和最後一天

2021-09-17 02:19:03 字數 1116 閱讀 4596

因為專案開發中遇到需要向後台傳本週的開始和結束時間,以及上一周的起止時間,就琢磨了半天,總算寫出來一套,寫篇文章是為了方便自己記憶,也是分享給需要的人,水平有限,寫的不好請見諒:

//獲取當前日期yy-mm-dd

//date 為時間物件

function getdatestr3(date) else

if((now.getdate())<10)else

return year+"-"+month+"-"+day;

}/**

* 獲得相對當前周addweekcount個周的起止日期

* **/

function getweekstartandend(addweekcount)

/**

* 獲得相對當月addmonthcount個月的起止日期

* ***/

function getmonthstartandend(addmonthcount)

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;

}

C 獲取周的第一天 最後一天 月第一天和最後一天

獲取指定日期所在周的最後一天,星期天為最後一天 public static datetime getdatetimeweeklastdaysun datetime datetime catch return lastweekday 獲取指定日期的月份第一天 public static datetim...

計算每週第一天和最後一天

計算每週第一天和最後一天 select day,dayofweek day as dw1 date add day,1 dayofweek day as su s 週日 start date add day,7 dayofweek day as sa e 週六 end case when dayof...

C DateTime 月第一天和最後一天 取法

取得某月和上個月第一天和最後一天的方法 取得某月的第一天 要取得月份第一天的時間 private datetime firstdayofmonth datetime datetime 取得某月的最後一天 要取得月份最後一天的時間 private datetime lastdayofmonth dat...