JS 獲取當前月份的第一天和最後一天

2021-08-30 02:07:26 字數 650 閱讀 8665

在 js 中,new date 函式,可以傳遞進去乙個時間戳,返回的時候時間戳對應的時間,也可以傳遞進去乙個日期,得到時間戳。

該方法用的就是這個。

var now = new date();

var month = now.getmonth() + 1;//js獲取到的是月份是 0-11 所以要加1

var year = now.getfullyear();

var nextmonthfirstday = new date([year,month + 1,1].join('-')).gettime();

var oneday = 1000 * 24 * 60 * 60;

var monthlast = new date(nextmonthfirstday - oneday).getdate()

console.log([year,month,1].join('-'))

console.log([year,month,monthlast].join('-'))

now 當前時間

month 當前月份

year 當前年份

nextmonthfirstday 下月第一天的時間戳

利用下月第一天的時間戳,減去一天的時間戳,得到的是上月最後一天時間戳

js 獲取當前月份 第一天和最後一天

js 獲取當前月份 第一天和最後一天 var now new date 當前日期 var nowmonth now.getmonth 當前月 var nowyear now.getfullyear 當前年 本月的開始時間 var monthstartdate new date nowyear,now...

獲取月份的第一天和最後一天

在做資料統計的時候會用到按月統計的問題 總結歸納 一般下個月初會統計上個月的一些運營資料,這樣就需要月初和月末的情況 1.獲取上個月第一天及最後一天.echo date y m d strtotime date y m 01 1 month 計算出本月第一天再減乙個月 上個月最後一天 echo da...

獲取當前月的第一天和最後一天

使用到 day.js import dayjs form js day.js firstday 第一天 lastday 最後一天 獲取當前時間 let date newdate 獲取當前月的第一天 let monthstart date.setdate 1 獲取當前月 let currentmont...