Get last date of month of passed date

Viewed 48
  • I have date like 2022-09-18T18:30:00Z
  • I want last date of the month
  • I'm using below snippet
moment(`2022-09-18T18:30:00Z`).clone().endOf('month').utc()

I'm using Node.js and it's working in my local, but not in my live server. It doesn't gives me UTC time instead gave me 2022-09-30 23:59:59

1 Answers

Without moment.js you can just set the UTC date to the last day of the month, e.g.

let d = new Date('2022-09-18T18:30:00Z');

d.setUTCMonth(d.getUTCMonth() + 1, 0);

console.log(d.toISOString()); // 2022-09-30T18:30:00.000Z

Related