12-26-2022, 04:54 AM
We will get date in yyyy-mm-dd format
Indexing starts from zero for month
var date = new Date();
var curr_month = date.getMonth();
var currentYear = date.getFullYear();
function padTo2Digits(num) {return num.toString().padStart(2, '0');}
function cur_month(date) {
return [
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-');
}
if (curr_month==0)
{var pre_month1 = 11;
var currentYear1 = currentYear-1;}
else {var pre_month1 =curr_month-1 ;
var currentYear1 = currentYear;}
var premonth_start = cur_month(new Date(currentYear1, pre_month1, 1)) );
var premonth_end = cur_month(new Date(currentYear1, pre_month1 + 1, 0)) );
Indexing starts from zero for month
var date = new Date();
var curr_month = date.getMonth();
var currentYear = date.getFullYear();
function padTo2Digits(num) {return num.toString().padStart(2, '0');}
function cur_month(date) {
return [
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-');
}
if (curr_month==0)
{var pre_month1 = 11;
var currentYear1 = currentYear-1;}
else {var pre_month1 =curr_month-1 ;
var currentYear1 = currentYear;}
var premonth_start = cur_month(new Date(currentYear1, pre_month1, 1)) );
var premonth_end = cur_month(new Date(currentYear1, pre_month1 + 1, 0)) );