Common date calculations in js

1. A date is known in js as var date='2023-01'; How to calculate the last day of this month, you need to consider the size of the month, February has already been a leap year, and you need to consider that adding 1 to December will change to the next year situation

var date = '2023-01';
var dateObj = new Date(date + '-01');
dateObj.setMonth(dateObj.getMonth() + 1);
dateObj.setDate(0);
var lastDayOfMonth = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate();
return lastDayOfMonth;

2. Already Quarter 2023-1 Quarter

year 2023-1 quarter
    year = year.replace('quarter', '');
    var split = year.split('-');
    var quarter = parseInt(split[1]); // get the quarter
    var year = parseInt(split[0]); // get the year
    var firstMonth = (quarter - 1) * 3 + 1; // Calculate the first month of the quarter
    var lastMonth = quarter * 3; // Calculate the last month of the quarter
    var firstDayOfQuarter = new Date(year, firstMonth - 1, 1); // first day of quarter
    var lastDayOfQuarter = new Date(year, lastMonth, 0); // last day of quarter
    startDate = firstDayOfQuarter.getFullYear() + '-' + (firstDayOfQuarter.getMonth() + 1) + '-' + firstDayOfQuarter.getDate();
    endDate = lastDayOfQuarter.getFullYear() + '-' + (lastDayOfQuarter.getMonth() + 1) + '-' + lastDayOfQuarter.getDate();

Supongo que te gusta

Origin blog.csdn.net/m0_37609579/article/details/130216465
Recomendado
Clasificación