js獲取上個月的今天或者下個月的今天

2021-09-28 16:13:38 字數 1031 閱讀 8511

如果上個月或者下個月沒有今天,就取上個月或者下個月的最後一天。

傳引數 1是下個月的今天,-1是取上個月的今天。

function getxmonthtoday(type) 

if (parseint(day) < 10)

now = year + '-' + month + '-' + day; // 如果取當月日期可直接 return 返回

var premonth = parseint(month) - 1;

premonth = premonth < 10 ? '0' + premonth : premonth; // 獲取上個月的值

var nextmonth = parseint(month) + 1;

nextmonth = nextmonth < 10 ? '0' + nextmonth : nextmonth; // 獲取下個月個月的值

if (parseint(month) == 1 && type == -1) else if (parseint(month) == 12 && type == 1)

var presize = new date(year, parseint(month) - 1, 0).getdate();//上月總天數

var nextsize = new date(year, parseint(month)+ 1, 0).getdate();//下月總天數

console.log(presize, nextsize)

if (presize < parseint(day) && type == -1) else if (nextsize < parseint(day) && type == 1)

if (type == -1) else if (type == 1) else if(type == 0)

}// 測試

let a = getxmonthtoday(-1);

let b = getxmonthtoday(1);

console.log(a,b)

JS獲取下個月或上個月的今天

date.prototype.format function fmt if y test fmt for var k in o return fmt 呼叫方式 var time1 new date format yyyy mm dd hh mm ss 為了方便就封裝成傳遞乙個天數,即可獲取多少天後的...

python中計算上個月和下個月的第一天的方法

鬧騰,乙個簡單的東西複雜化了,記錄下吧 import datetime,time defget 1st of last month 獲取上個月第一天的日期,然後加21天就是22號的日期 return 返回日期 today datetime.datetime.today year today.year...

下個月的目標

第二個目標就是就是好好的理財,學習理財早日走上投資的這條路,如果有可能這一年給自己上保險。當然,這條很遠,我知道這條路的終點,我已經預見了,我總是感覺在未來的某一天,我能實現小小的財務自由,我可以靠理財生活,我不需要靠公司生活,我不想被所謂的公司牢靠一生,我還有要追求的事情。其實我不在乎什麼公升職,...