I had an issue with Date:
I want to get the last day of month
const date = new Date();
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() 1, 0);
console.log(lastDayOfMonth); //Wed Nov 30 2022 00:00:00 GMT 0000 (temps universel coordonné)
console.log(lastDayOfMonth.toISOString()) //2022-11-30T00:00:00.000Z
The same code I've run on other computer's browser
I found the same result except console.log(lastDayOfMonth.toISOString()) //2022-11-29T00:00:00.000Z
, I got 29
instead of 30
?
I don't know why? if anyone knows, could explain us more why Date behave differently on different browser...
CodePudding user response:
var date = new Date();
date.setMonth(date.getMonth() 1);
date.setDate(0);
The setDate with 0 gives back the previous month day
CodePudding user response:
const date = new Date();
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() 1, 0);
console.log(lastDayOfMonth); //Wed Nov 30 2022 00:00:00 GMT 0000 (temps universel coordonné)
console.log(lastDayOfMonth.toISOString()) //2022-11-30T00:00:00.000Z