Skip to content

Instantly share code, notes, and snippets.

@jongan69
Created February 1, 2023 23:07
Show Gist options
  • Select an option

  • Save jongan69/67a58e2787bc77aec20acfc39439b11d to your computer and use it in GitHub Desktop.

Select an option

Save jongan69/67a58e2787bc77aec20acfc39439b11d to your computer and use it in GitHub Desktop.
Returns if true a month ends mid week and false if not

Split Weeks

This code finds split weeks for a given year.

Usage

To use the code, define year as the year to find split weeks for. Then call the getMonthlyDates and isSplitWeek functions.

let year = new Date().getFullYear()
console.log(`Finding Split weeks for year ${year}`)

let lastDates = getMonthlyDates(new Date(year, 0, 31), 12);

lastDates.forEach(d => {
  console.log(`is split week:`, isSplitWeek(d));
});

The getMonthlyDates function takes a start date and a count and returns the last day of each month for the specified range, starting from the start date.

The isSplitWeek function takes a date and returns true or false, depending on if it is a split week or not.

Example

Using the code above, the output for 2020 would look like this:

Finding Split weeks for year 2020
Tue Dec 31 2019 00:00:00 GMT-0800 (Pacific Standard Time)
Week ends on Tuesday:
is split week: true 

Wed Jan 29 2020 00:00:00 GMT-0800 (Pacific Standard Time)
// Need to have current year
let year = new Date().getFullYear()
console.log(`Finding Split weeks for year ${year}`)
// For each month return last day of month for start to end date
function getMonthlyDates(start, count) {
var result = [];
var temp;
var year = start.getFullYear();
var month = start.getMonth();
var startDay = start.getDate();
for (var i = 0; i < count; i++) {
temp = new Date(year, month + i, startDay);
if (temp.getDate() != startDay) temp.setDate(0);
result.push(temp);
}
return result;
}
// given end of month day return true if split week and false if not
function isSplitWeek(day) {
const lastDayOfWeek = day.getDay();
// Sunday - Saturday : 0 - 6
daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
console.log(`Week ends on ${daysOfWeek[lastDayOfWeek]}:`);
switch(lastDayOfWeek){
case 0:
return false
case 1:
return true
case 2:
return true
case 3:
return true
case 4:
return true
case 5:
return false
case 6:
return false
}
}
let lastDates = getMonthlyDates(new Date(year, 0, 31), 12);
lastDates.forEach(d => {
console.log(d.toString())
console.log(`is split week:`, isSplitWeek(d));
console.log(' ')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment