Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created January 4, 2023 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/1153792b17ce29ae86c865e53c999375 to your computer and use it in GitHub Desktop.
Save tanaikech/1153792b17ce29ae86c865e53c999375 to your computer and use it in GitHub Desktop.
Retrieving Start and End of Month in Year using Google Apps Script and Javascript

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

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"]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment