Last date of month

JavaScript, Date · Oct 9, 2020

Returns the string representation of the last date in the given date's month.

  • Use Date.prototype.getFullYear(), Date.prototype.getMonth() to get the current year and month from the given date.
  • Use the Date constructor to create a new date with the given year and month incremented by 1, and the day set to 0 (last day of previous month).
  • Omit the argument, date, to use the current date by default.
const lastDateOfMonth = (date = new Date()) => {
  let d = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  return d.toISOString().split('T')[0];
};
lastDateOfMonth(new Date('2015-08-11')); // '2015-08-30'

More like this

  • Number of seconds to ISO format

    Returns the ISO format of the given number of seconds.

    JavaScript, Date · Oct 13, 2021

  • Add minutes to date

    Calculates the date of n minutes from the given date, returning its string representation.

    JavaScript, Date · Nov 28, 2020

  • Add days to date

    Calculates the date of n days from the given date, returning its string representation.

    JavaScript, Date · Nov 28, 2020