Retrieving Start and End of Month in Year using Google Apps Script and Javascript

Gists

This is a sample script for retrieving the start and end of the month in a year using Google Apps Script and Javascript.

Sample script

function myFunction() {
  const year = 2023; // Please set year you expect.
  const res = [...Array(12)].map((_, i) =>
    [0, 1].map((e) => new Date(year, i + e, 1 - e))
  );
  console.log(res);

  console.log(res.map(([a, b]) => [a.toDateString(), b.toDateString()]));
}

Testing

https://jsfiddle.net/mLrhqwgo/

When this script is run, the following value is obtained with console.log(res.map(([a, b]) => [a.toDateString(), b.toDateString()])).

[
  ["Sun Jan 01 2023", "Tue Jan 31 2023"],
  ["Wed Feb 01 2023", "Tue Feb 28 2023"],
  ["Wed Mar 01 2023", "Fri Mar 31 2023"],
  ["Sat Apr 01 2023", "Sun Apr 30 2023"],
  ["Mon May 01 2023", "Wed May 31 2023"],
  ["Thu Jun 01 2023", "Fri Jun 30 2023"],
  ["Sat Jul 01 2023", "Mon Jul 31 2023"],
  ["Tue Aug 01 2023", "Thu Aug 31 2023"],
  ["Fri Sep 01 2023", "Sat Sep 30 2023"],
  ["Sun Oct 01 2023", "Tue Oct 31 2023"],
  ["Wed Nov 01 2023", "Thu Nov 30 2023"],
  ["Fri Dec 01 2023", "Sun Dec 31 2023"]
]

 Share!