Return week start date (Monday) in Google Script

1,669 views
Skip to first unread message

Gilberto J. Colin

unread,
Mar 5, 2022, 8:32:57 AM3/5/22
to Google Apps Script Community
hello, can someone please be a lifesaver and help me troubleshoot the below code?
for some reason, the code below returns Sunday instead of Monday week start date

 /* return start of the week (Monday) based on current date  */

  var curr = new Date();
  var first = (curr.getDate() - curr.getDay()) ;
  var Monday = Utilities.formatDate(new Date(curr.setDate(first)), "UTC", "YYYY-MM-dd");
  var sh = SpreadsheetApp.getActiveSheet();
  
 sh.getRange("A3").setValue(Monday) ;
  
};



Clark Lind

unread,
Mar 6, 2022, 2:07:41 PM3/6/22
to Google Apps Script Community
Hello, 
The Google "week" starts on Sunday: Sunday=0, Monday=1, Sat=6.
So I think the only modification would be to subtract 1 from the day:
var first = (curr.getDate() - (curr.getDay() - 1) ) ;
Reply all
Reply to author
Forward
0 new messages