Skip to content

Instantly share code, notes, and snippets.

@cwardzala
Created April 21, 2017 12:46
Show Gist options
  • Save cwardzala/a8bd98662f1e4f46ef657b981c6e8cd9 to your computer and use it in GitHub Desktop.
Save cwardzala/a8bd98662f1e4f46ef657b981c6e8cd9 to your computer and use it in GitHub Desktop.
function getSopMonth (asText = false) {
let date = new Date();
let month = date.getUTCMonth() + 1; // date.getMonth() is zero indexed.
let names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
if (asText) {
return names[month];
}
return month + 1; // zero index so we get the correct month integer.
}
function getSopQuarter () {
return getQuarter((new Date()).getUTCMonth() + 1);
}
function getQuarter (month) {
let quarters = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]];
let words = ['First', 'Second', 'Third', 'Fourth'];
let q = Math.ceil(month / 3);
return {
name: words[q - 1],
range: quarters[q - 1],
number: q
}
}
function isOpen (sop) {
if (!sop.last_submitted) return true;
// Get current date
let current = new Date();
// Get first day of the month
let first_day = new Date(current.getFullYear(), current.getUTCMonth(), 1);
// Get last submitted date
let last_date = new Date(`${sop.last_submitted.split('T')[0]}T00:00:00`); // Reset time on date to prevent time mismatch issues.
if (sop.frequency === 'quarterly') {
// Get actual month integer
let current_month = current.getUTCMonth() + 1;
// Get quarter based on month
let quarter = this.getQuarter(current_month);
return quarter.range.indexOf(last_date.getUTCMonth()) === -1 // last submitted month is not in current quarter
&& quarter.range.indexOf(current_month) > 0 // current month index in quarter is greater than 0 (second or third)
&& last_date < first_day; // last submitted date is before the first day of the current month
}
return last_date < first_day; // monthly sub last submitted date is before the first day of the current month
}
console.log(getSopQuarter());
console.log('Quarterly (2017-03-15) is open? %s', isOpen({
last_submitted: '2017-03-15',
frequency: 'quarterly'
}));
console.log('Monthly (2017-03-15) is open? %s' , isOpen({
last_submitted: '2017-03-15',
frequency: 'monthly'
}));
console.log('Monthly (2017-04-01) is open? %s' , isOpen({
last_submitted: '2017-04-01',
frequency: 'monthly'
}));
console.log('Monthly (2017-04-15) is open? %s' , isOpen({
last_submitted: '2017-04-15',
frequency: 'monthly'
}));
console.log('Null date is open? %s' , isOpen({
last_submitted: null,
frequency: 'monthly'
}));
console.log('Empty string date is open? %s' , isOpen({
last_submitted: '',
frequency: 'monthly'
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment