Home > Software engineering >  Listing out each date in a given month
Listing out each date in a given month

Time:06-15

I'm trying to write a function in Apps Script where I can input a date and the output will be an array of dates for each day in the month.

ex.

 function("random date in april 2022"){}

output: {2022-04-01, 2022-04-02, etc.}

CodePudding user response:

You can do that with a plain vanilla spreadsheet formula without resorting to scripting, like this:

=arrayformula( eomonth(A2, -1) sequence(day(eomonth(A2, 0))) )

Since you specified Apps Script, here is a custom function to do the same:

/**
* Returns a list of all dates in the month of the parameter date.
*
* @param {Date} date The date that determines which month's dates to list.
* @return {Date[][]} The list of dates in the month.
* @customfunction
*/
function ListMonthDays(date = new Date()) {
  const month = date.getMonth();
  const result = [];
  for (let i = 1; i <= 31; i  ) {
    date.setDate(i);
    if (date.getMonth() === month) {
      result.push([new Date(date)]);
    }
  }
  return result;
}

CodePudding user response:

Monthly Calendar for a Year

function calendarMonthlyForYear() {
  var dA = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  var oA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];//you can change the first day of the week by shifting the array
  //var oA=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  var dObj = {};
  oA.forEach(function (e, i) { dObj[e] = i });
  var mA = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  var cA = [];
  var bA = [];
  var wA = [null, null, null, null, null, null, null];
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("Cal");
  if (!sh) { var sh = ss.insertSheet('Cal'); }
  sh.clear();
  var year = new Date().getFullYear();
  for (var i = 0; i < 12; i  ) {
    var month = new Date(year, i, 1).getMonth();
    var dates = new Date(year, i   1, 0).getDate();
    cA.push([mA[month], dates, '', '', '', '', '']);
    bA.push(['#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff']);
    cA.push(oA)
    bA.push(['#ffff00', '#ffff00', '#ffff00', '#ffff00', '#ffff00', '#ffff00', '#ffff00']);
    var d = [];
    var ddd = [];
    for (var j = 0; j < dates; j  ) {
      var day = new Date(year, i, j   1).getDay();
      var date = new Date(year, i, j   1).getDate();
      if (day < wA.length) {
        wA[dObj[dA[day]]] = date;
      }
      //if(day==wA.length-1 || date==dates) {
      if (dA[day] == oA[wA.length - 1] || date == dates) {
        cA.push(wA);
        bA.push(['#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff', '#ffffff']);
        wA = ['', '', '', '', '', '', ''];
      }
    }
  }
  sh.getRange(1, 1, cA.length, cA[0].length).setValues(cA);
  //sh.getRange(1,1,cA.length,cA[0].length).setBackgrounds(bA);
  sh.getRange(1, 1, cA.length, cA[0].length).setBackground('#ffffff');
}
Jan 31
Mon Tue Wed Thu Fri Sat Sun
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Feb 28
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
Mar 31
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Apr 30
Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
May 31
Mon Tue Wed Thu Fri Sat Sun
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Jun 30
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Jul 31
Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Aug 31
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Sep 30
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Oct 31
Mon Tue Wed Thu Fri Sat Sun
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Nov 30
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Dec 31
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  • Related