Reputation: 171
I am trying to figure out the 1st date and last date of the current week as well as the 1st date and last date of the current month in the format yyyy-mm-dd
using Google App scripts. Date()
seems to be a difficult object for me, therefore seeking advice. Please consider Saturday is the starting day and Friday is the end day of a week in this parts of the world.
Upvotes: 0
Views: 3082
Reputation: 27390
You can use toLocaleString("sv-SE") to convert the date to the desired format yyyy-mm-dd
.
Try this:
const today = new Date();
const firstWeekD = today.getDate() - today.getDay()-1;
const lastWeekD = firstWeekD + 6;
const firstdayOfWeek = new Date(today.setDate(firstWeekD)).toLocaleString("sv-SE").slice(0,10);
const lastdayOfWeek = new Date(today.setDate(lastWeekD)).toLocaleString("sv-SE").slice(0,10);
const date = new Date();
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1).toLocaleString("sv-SE").slice(0,10);
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).toLocaleString("sv-SE").slice(0,10);
console.log(firstdayOfWeek); // first day of the week - Saturday
console.log(lastdayOfWeek); // last day of the week - Friday
console.log(firstDayOfMonth) // first day of the month
console.log(lastDayOfMonth) // last day of the month
Upvotes: 2
Reputation: 2072
You can use Date.getDay()
to get the current day of the week (0=Sunday, 1=Monday, etc.). But there is no method Date.setDay()
so we need to do a little math to figure out how many days ago the most recent Saturday was, and use Date.setDate()
:
// Get the Date for the Saturday on or preceding the current date.
var saturday = new Date(); // Today
var day = date.getDay(); // Day of week (0-6)
var newDay = date.getDate() - ((day + 1) % 7); // The day of the month we want.
saturday.setDate(newDay); // Our date object for Saturday.
(day + 1) % 7
calculates how many days to subtract; for example, if today is Sunday (day=0), we need to go back one day: (0 + 1) % 7 = 1
. But if today is Saturday (day=6), we get (6 + 1) % 7 = 0
and we don't subtract any days.
The routine for finding the upcoming Friday is similar, but we add days instead of subtracting:
// Get the Date for the Friday on or after the given date.
var friday = new Date(date); // today
var day = date.getDay();
var diff = date.getDate() + ((5 - day) % 7);
friday.setDate(diff);
This is simpler using setDate()
, since it takes as its parameter the day of the month.
var firstOfMonth = new Date(); // today
firstOfMonth.setDate(1); // Sets the day to the first of the month
To get the last of the month, we go ahead one month, then back to the last day of the previous month (the last of this month):
var lastOfMonth = new Date();
lastOfMonth.setMonth(lastOfMonth.getMonth()+1); // Go ahead one month
lastOfMonth.setDate(0); // Sets to 1 day before the first of the month, i.e. the last of this month.
Google Apps Script has a built-in method for formatting dates: Utilities.formatDate(date, timezone, format)
date
is a Date object, timezone
is a string such as those from the TZ database, and format
is a string in SimpleDateFormat
For example:
var formattedDate = Utilities.formatDate(friday, 'America/Denver', 'yyyy-MM-dd');
(Note the capital MM
for month because mm
represents minutes.)
Upvotes: 3