开发中的一个小需求,用js获取获取上个月、当月、下个月 第一天与最后一天,直接上代码
/**
* 获取上个月、当月、下个月 第一天与最后一天
* @param val 传值 ->1/2/3/""
* @param specificDate 传入日期 ->(2020-11-01 00:00:00)/(2020/11/01 00:00:00)/(2020/11/01)/(2020-11-01)
* @example
*
* const aaa= getLastMonthCurrentMonthNextMonth()
* console.log(aaa)-> ['2023-02-01', '2023-02-28', '2023-03-01', '2023-03-31', '2023-04-01', '2023-04-30']
* const bbb= getLastMonthCurrentMonthNextMonth(1)
* console.log(bbb)-> ['2023-02-01', '2023-02-28']
* const ccc= getLastMonthCurrentMonthNextMonth(2)
* console.log(ccc)-> ['2023-03-01', '2023-03-31']
* const ddd= getLastMonthCurrentMonthNextMonth(3)
* console.log(ddd)-> ['2023-04-01', '2023-04-30']
* const eee= getLastMonthCurrentMonthNextMonth('','')
* console.log(eee)-> ['2023-02-01', '2023-02-28', '2023-03-01', '2023-03-31', '2023-04-01', '2023-04-30']
* const fff= getLastMonthCurrentMonthNextMonth(1,'2023-01-01')
* console.log(fff)-> ['2022-12-01', '2022-12-31']
* const ggg= getLastMonthCurrentMonthNextMonth("",'2023-01-01')
* console.log(ggg)-> ['2022-12-01', '2022-12-31', '2023-01-01', '2023-01-31', '2023-02-01', '2023-02-28']
*
*
*/
export function getLastMonthCurrentMonthNextMonth(val,specificDate){
// console.log("------获取上个月第一天和最后一天年月日---------");
let LastDate = specificDate ? new Date(specificDate) : new Date();
LastDate.setDate(0);
let LastYear = LastDate.getFullYear(); //获取年份
let LastMonth = LastDate.getMonth() + 1; //获取月份
LastMonth = LastMonth < 10 ? "0" + LastMonth : LastMonth; //月份补 0
let LastMonthDay = new Date(LastYear, LastMonth, 0).getDate(); //获取当月最后一日
LastMonthDay = LastMonthDay < 10 ? "0" + LastMonthDay : LastMonthDay; //日数补 0
let LastMonthFirstDay=[LastYear, LastMonth, '01'].join("-")
let LastMonthEndDay=[LastYear, LastMonth, LastMonthDay].join("-")
// console.log("-------获取当月的第一天和最后一天的年月日--------------");
let currentDate = specificDate ? new Date(specificDate) : new Date();
let currentYear = currentDate.getFullYear(); //取当前的年份
let currentMonth = currentDate.getMonth()+1
currentMonth = currentMonth < 10 ? "0" + currentMonth : currentMonth; //月份补 0
let currentMonthLastday = new Date(currentYear, currentMonth, 0).getDate(); //获取当月最后一日
let currentFirstDay=[currentYear, currentMonth, '01'].join("-")
let currentEndDay=[currentYear, currentMonth, currentMonthLastday].join("-")
// console.log("---------获取下个月第一天和最后一天年月日-----------");
let NextDate = specificDate ? new Date(specificDate) : new Date();
let NextDateYear = NextDate.getFullYear(); //获取年份
let NextDateMonth = NextDate.getMonth() + 1; //获取月份
let NextMonth=''
let NextYear=''
if (NextDateMonth+1>11){
NextMonth=1
NextYear=NextDateYear+1
}else{
NextMonth =NextDateMonth+1
NextYear=NextDateYear
}
NextMonth = NextMonth < 10 ? "0" + NextMonth : NextMonth; //月份补 0
let NextMonthLastday = new Date(NextYear, NextMonth, 0).getDate(); //获取当月最后一日
let NextMonthFirstDay=[NextYear, NextMonth, '01'].join("-")
let NextMonthEndDay=[NextYear, NextMonth, NextMonthLastday].join("-")
switch (val) {
case 1:
return[LastMonthFirstDay,LastMonthEndDay]
break;
case 2:
return[currentFirstDay,currentEndDay]
break;
case 3:
return[NextMonthFirstDay,NextMonthEndDay]
break;
default:
return[LastMonthFirstDay,LastMonthEndDay,currentFirstDay,currentEndDay,NextMonthFirstDay,NextMonthEndDay]
break;
}
}