I'm from Europe and there we have the summertime. It is +1 hour every date between two dates. These dates are the last Saturday of March and the last of oktober. I'm trying to make my program automatic realize the summertime. And for that I have to get the last Sunday of a month.
I found this: java last sunday of a month but I was not able to translate it into JavaScript.
I'm from Europe and there we have the summertime. It is +1 hour every date between two dates. These dates are the last Saturday of March and the last of oktober. I'm trying to make my program automatic realize the summertime. And for that I have to get the last Sunday of a month.
I found this: java last sunday of a month but I was not able to translate it into JavaScript.
Share Improve this question edited Aug 17, 2020 at 17:08 Player Schark asked Aug 17, 2020 at 17:01 Player ScharkPlayer Schark 1271 silver badge10 bronze badges 1- 1 rosettacode/wiki/… – Njuguna Mureithi Commented Aug 17, 2020 at 17:12
3 Answers
Reset to default 5For getting the last Sunday of a month, create a new date for the first of next month (so take month, because in JS month is 0-11). For the actual month you had to subtract 1 for the previous month and plus 1 for next month at 12:00 to avoid conflicts with summer/wintertime.
Get the weekday with getDate
(note 0-6 in Javascript is from Sunday to Saturday) and if this day is a Sunday (=0) then assign 7. Build a new date from the first of next month, subtract the day difference, and set it as new date. By using this trick you get the last Sunday of a month.
If you only want the day instead of the date-string then just use lastSunday.getDate()
.
Note: If you use the date-object think of that the time is normally set to 12:00 but through sumer/wintertimechange it could be 11:00 or 13:00. But this shouldn't matter because you wanted only the date and not the time.
function lastSunday(year, month) {
var date = new Date(year,month,1,12);
let weekday = date.getDay();
let dayDiff = weekday===0 ? 7 : weekday;
let lastSunday = date.setDate(date.getDate() - dayDiff);
return date.toDateString();
}
for (let i=1; i<=12; i++) {
console.log(lastSunday(2020,i));
}
You can get the last day of the month, then subtract the day number, e.g.
/* @param {number|string} year - year to use
** @param {number|string} month - calendar month number
** @returns {Date} last Sunday for month in given year
*/
function getLastSunday(year, month) {
let d = new Date(year, month, 0);
d.setDate(d.getDate() - d.getDay());
return d;
}
// Get all last Sundays for current year
for (var y=new Date().getFullYear(), i=1; i<=12; i++) {
console.log(getLastSunday(y, i).toString());
}
Because ECMAScript month numbers are zero based, using the calendar month number in the Date constructor gives the next month, so 8 for August is treated as 9 for September. Setting the day to 0 makes it one day before the 1st so September 0 is August 31.
Similarly, the day numbers are 0 to 6 for Sunday to Saturday, so if the date isn't a Sunday, subtracting the day number moves the date to the previous Sunday.
Near future…
There is a new Temporal object in development that seeks to make this stuff a bit easier. A function to get the last Sunday of a month might be:
/**
* Returns last Sunday of month
*
* @param {Temporal.YearMonth} queryMonth - YearMonth to get last Sunday of
* @returns {Temporal.Date} for last Sunday of queried month
*/
function getLastSunday(queryMonth) {
let lastOfMonth = queryMonth.toDateOnDay(queryMonth.daysInMonth);
return lastOfMonth.minus({days: lastOfMonth.dayOfWeek % 7});
}
// As a Temporal.Date
getLastSunday(Temporal.YearMonth.from('2020-08'));
// As a string like Sun, Aug 30
getLastSunday(Temporal.YearMonth.from('2020-08')).toLocaleString('en', {
weekday: 'short',
day: '2-digit',
month: 'short'
});
Unfortunately it relies on the Intl object for formatting, so there will still be a need for formatting (and parsing) libraries. It would be nice if it included a simple formatter using one of the many token sets currently in use.
I think this code includes the solution ("last week sunday"):
http://jsbin./yevutabuxe/edit?js,console