Skip to content

Instantly share code, notes, and snippets.

@sonyseng
Last active March 11, 2022 06:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sonyseng/4f81aa204852157296c16df668e76beb to your computer and use it in GitHub Desktop.
Save sonyseng/4f81aa204852157296c16df668e76beb to your computer and use it in GitHub Desktop.
Given a Gregorian Date, get the Broadcast Date Ranges for Week, Month, Quarter, and Year
(function (url, onLoad) {
var script = document.createElement('script');
script.addEventListener('load', onLoad);
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}('https://cdn.jsdelivr.net/npm/moment@2.19.2/moment.min.js', function () {
const fmt = 'MM/DD/YYYY';
function getBroadcastWeek(momentDate) {
let date = momentDate.clone();
let day = date.day();
// Find the closest Monday <= the date
let start = moment(date).subtract((day + 6) % 7, 'day');
// Find the closest Sunday >= the date
let end = moment(date).add((7 - day) % 7, 'day');
return { start, end };
}
function getBroadcastMonth(momentDate) {
// Given the start of the Gregorian month is always included in the start of the
// broadcast month, we can use the broadcast week method to find it.
let startDate = momentDate.clone().startOf('month');
// The broadcast end date in the month is always the last Sunday of the month
let endDate = momentDate.clone().endOf('month');
let { start } = getBroadcastWeek(startDate);
let end = endDate.subtract(endDate.day(), 'day');
return { start, end };
}
function getBroadcastQuarter(momentDate) {
// Given that a broadcast first of month always contains the gregorian first of the month, each
// first day in a broadcast quarter contains the first day in the gregorian quarter.
let { end: date } = getBroadcastWeek(momentDate.clone());
let { start } = getBroadcastWeek(date.startOf('quarter'));
let { end } = getBroadcastMonth(date.add(2, 'months'));
return { start, end };
}
function getBroadcastYear(momentDate) {
// Similar logic. We use the start of the month to get broadcast dates
let { start } = getBroadcastWeek(momentDate.clone().startOf('year'));
let { end } = getBroadcastMonth(momentDate.clone().month(11).startOf('month'));
return { start, end };
}
// TEST
let date = moment('01/11/2018', fmt);
let { start: weekStart, end: weekEnd } = getBroadcastWeek(date);
let { start: monthStart, end: monthEnd } = getBroadcastMonth(date);
let { start: quarterStart, end: quarterEnd } = getBroadcastQuarter(date);
let { start: yearStart, end: yearEnd } = getBroadcastYear(date);
console.log('Date:', date.format(fmt));
console.log('Broadcast Week:', weekStart.format(fmt), '->', weekEnd.format(fmt));
console.log('Broadcast Month:', monthStart.format(fmt), '->', monthEnd.format(fmt));
console.log('Broadcast Quarter:', quarterStart.format(fmt), '->', quarterEnd.format(fmt));
console.log('Broadcast Year:', yearStart.format(fmt), '->', yearEnd.format(fmt));
}))
@sonyseng
Copy link
Author

Just some limited testing. Hopefully it's correct based on https://en.wikipedia.org/wiki/Broadcast_calendar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment