'Show Calendar events this week

I have a nice function that shows all events on my calendar for this month but I can't figure out how to change that to this week (monday to sunday).

I can change the list by changing date to today's date and adding days for example. I tried some things with Utilities.formatDate but this gives me weeknumbers, no dates. I have no clue how to get the current week.

function listEvents2() {
  var date = new Date();
  var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  var events2 = CalendarApp.getDefaultCalendar().getEvents(firstDay, lastDay);

  var data2 = [];
  data2.push("Deze maand: ");
  if (events2 && events2.length > 0) {
    for (i = 0; i < events2.length; i++) {
      data2.push(events2[i].getTitle()+' : '+Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"dd MMMM HH:mm")+' - '+Utilities.formatDate(events2[i].getEndTime(),Session.getScriptTimeZone(),"HH:mm"))
    }
    return data2;
  }
  else {
    return ['No events found','',''];
  }
}

I would like some tips how to achieve this.

Edit: Maybe I need to explain more. This function is used for a personal weekplanner dashboard with HtmlService. There is another function that gives a list of today's events.

Edit 2: Here is my working code:

function email() {


  var date = new Date();
  var day = date.getDay();
  var firstDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()-day+1);
  var lastDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()-day+8);
  var events2 = CalendarApp.getCalendarById(CalendarID).getEvents(firstDay, lastDay);

  var data2 = [];
  data2.push("<b>Title</b>");
  if (events2 && events2.length > 0) {
    for (i = 0; i < events2.length; i++) {
      data2.push(Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"EEEE dd/MM")+ ' ' + Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"HH:mm") +'<br/>'+events2[i].getTitle())
    }

    return data2;
  } else {
    return ['Nothing to do'];
  }
}

I decided to split the lists per calendar to have it more organized. But both answers below work.

There are still two things I need to figure out. First one is how I can get

Session.getScriptTimeZone(),"EEEE dd/MM")

to show EEEE in my native language instead of English.

The other one is that the function gives a perfect list of things to do but it skips empty days. I would like to display the empty days as the dashboard should function like a calendar view planner.

Thanks for the help so far!



Solution 1:[1]

In order to get the dates of the start and the end of the week, you can use the following, modified code:

function listEvents2() {
  var date = new Date();
  var firstDay = new Date();
  var lastDay = new Date();
  firstDay.setDate(firstDay.getDate() - firstDay.getDay() + 1);
  firstDay.setHours(0,0,0,0);
  lastDay.setDate(lastDay.getDate() - lastDay.getDay() + 8);
  lastDay.setHours(0,0,0,0);
  var events2 = CalendarApp.getDefaultCalendar().getEvents(firstDay, lastDay);

  var data2 = [];
  data2.push("Deze maand: ");
  if (events2 && events2.length > 0) {
    for (i = 0; i < events2.length; i++) {
      data2.push(events2[i].getTitle()+' : '+Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"dd MMMM HH:mm")+' - '+Utilities.formatDate(events2[i].getEndTime(),Session.getScriptTimeZone(),"HH:mm"))
    }
    return data2;
  }
  else {
    return ['No events found','',''];
  }
}

Bear in mind that these dates will take into consideration the timezone that you are in. If you want to work with dates in an easier way, I recommend you check out the library momentjs.

Also, if you want to use getAllCalendars(), you have to take into consideration that the return value of that function is an array of calendars rather than a calendar object (see the documentation). In order to list the events on each calendar, you can again modify the code in the following manner (both modifications):

function listEvents2() {
  var date = new Date();
  var firstDay = new Date();
  var lastDay = new Date();
  firstDay.setDate(firstDay.getDate() - firstDay.getDay() + 1);
  firstDay.setHours(0,0,0,0);
  lastDay.setDate(lastDay.getDate() - lastDay.getDay() + 8);
  lastDay.setHours(0,0,0,0);
  var events2 = [];
  var allCalendars = CalendarApp.getAllCalendars();
  for (var i=0; i<allCalendars.length; i++) {
    var calendarEvents = allCalendars[i].getEvents(firstDay, lastDay);
    events2 = events2.concat(events2, calendarEvents);
  }

  var data2 = [];
  data2.push("Deze maand: ");
  if (events2 && events2.length > 0) {
    for (i = 0; i < events2.length; i++) {
      data2.push(events2[i].getTitle()+' : '+Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"dd MMMM HH:mm")+' - '+Utilities.formatDate(events2[i].getEndTime(),Session.getScriptTimeZone(),"HH:mm"))
    }
    return data2;
  }
  else {
    return ['No events found','',''];
  }
}

Solution 2:[2]

You may find this related question useful. It helps you get the Monday (which can be changed to Sunday if you want your weeks to run Sunday-Saturday).

You can use that as your firstDay and then your lastDay is 6 days later than that, so something like:

var lastDay = new Date(firstDay.setDate(firstDay.getDate() + 6))

Be aware that in Javascript the .getDay() method returns 0 if it is a Sunday and 1 if it is Monday [0]. Likewise with months, January is 0, February is 1 etc. [1]

[0] https://www.w3schools.com/jsref/jsref_getday.asp

[1] https://www.w3schools.com/jsref/jsref_getmonth.asp

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 rsomji