Last active
July 20, 2016 19:44
-
-
Save Jarrid/878d08b1ff3dffe627cc932beff09a7c to your computer and use it in GitHub Desktop.
moment.js + bootstrap-datetimepicker functions to set an array of holidays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires moment.js | |
// for use with the datetime picker here https://eonasdan.github.io/bootstrap-datetimepicker | |
// these functions are meant to calculate the appropriate date for sliding holidays | |
// for example, Thanksgiving which occurs on the fourth Thursday in November. | |
// Not necessarily the same date every year. | |
// left a ton of console.logs so it can be seen in the browser | |
function getLastOccurringDayOfMonth(startDate,dayOfWeek) | |
{ | |
var dayNumber = moment().day(dayOfWeek).day(); | |
var found_date = moment(startDate).endOf('month').day(dayNumber); | |
// if this is still in the month provided, then return it! | |
if (found_date.month() == moment(startDate).month()){ | |
return found_date; | |
} | |
var found_date = moment(startDate).endOf('month').subtract(1,'w').day(dayNumber); | |
return found_date; | |
} | |
function getFirstOccurringDayOfMonth(startDate,dayOfWeek) | |
{ | |
var dayNumber = moment().day(dayOfWeek).day(); | |
var found_date = moment(startDate).startOf('month').day(dayNumber); | |
// if this is still in the month provided, then return it! | |
if (found_date.month() == moment(startDate).month()){ | |
return found_date; | |
} | |
var found_date = found_date.add(1,'w').day(dayNumber); | |
return found_date; | |
} | |
function getSlidingHoliday(startDate,whichWeek,dayOfWeek){ | |
var dayNumber = moment().day(dayOfWeek).day(); | |
//console.log('startDate ' + startDate); | |
// console.log('day number' + dayNumber); | |
var calculatedWeek = whichWeek; | |
if (calculatedWeek <= 0){ | |
var startOfMonth = moment(startDate).utc().startOf('month').startOf('isoweek').allDays(dayNumber); | |
return days[calculatedWeek]; | |
} | |
var startOfMonth = moment(startDate).utc().startOf('month'); | |
// console.log ( 'start of month day ' + startOfMonth.day()); | |
if (startOfMonth.day() == dayNumber){ | |
// console.log ('appears the start of the month and the requested day name are the same.') | |
var appropriate_date = moment(startDate).utc().startOf('month').add(whichWeek -1,'w').day(dayNumber); | |
// console.log('date returned is ' + appropriate_date.format('YYYY-MM-DD')); | |
return appropriate_date; | |
} | |
if (startOfMonth.day() > dayNumber){ | |
var weeksToUse = whichWeek-1; | |
// console.log ('start of month after the day number provided.'); | |
var result = moment(startDate).utc().startOf('month').format('YYYY-MM-DD'); | |
// console.log('the start of the month is on ' + result); | |
var result = moment(startDate).utc().startOf('month').format('YYYY-MM-DD'); | |
// console.log('this should use ' + weeksToUse.toString() + ' instead of ' + whichWeek.toString()); | |
var start_of_week = moment(startDate).utc().startOf('month').add(weeksToUse,'w'); | |
var result = moment(start_of_week).format('YYYY-MM-DD'); | |
// console.log('the appropriate week should be ' + result); | |
// console.log('but we need the next ' + dayOfWeek + ' and this is ' + start_of_week.format('dddd') ); | |
var appropriate_date = moment(startDate).utc().startOf('month').add(whichWeek,'w').day(dayNumber); | |
// console.log('so lets get the next monday...' + appropriate_date.format('YYYY-MM-DD')); | |
return appropriate_date; | |
} | |
// if here, then the start of the month is BEFORE the day name provided | |
// for example the day name of Monday was provided but the start of the week is on a Sunday. | |
var beginningOfMonth = startOfMonth.format('dddd'); | |
// console.log(' the beginning of the month is ' + beginningOfMonth + ' and the day needed is ' + dayNumber + ' which is after the start of the month.'); | |
var appropriate_date = moment(startDate).utc().startOf('month').add(whichWeek - 1,'w').day(dayNumber); | |
// console.log('so lets get the next monday...' + appropriate_date.format('YYYY-MM-DD')); | |
return appropriate_date; | |
} | |
var CurrentYear = moment().year(); | |
var NextYear = moment().add(1,'y').year(); | |
function addSlidingHolidayToArray(existingArray, dayPartial, whichWeek, dayName, holidayName){ | |
var current = getSlidingHoliday(CurrentYear + dayPartial, whichWeek,dayName); | |
var next = getSlidingHoliday(NextYear + dayPartial, whichWeek,dayName); | |
existingArray.push(current); | |
console.log( CurrentYear.toString() + ' ' + holidayName + ' set to ' + current.format('YYYY-MM-DD')); | |
existingArray.push(next); | |
console.log( NextYear.toString() + ' ' + holidayName + ' set to ' + next.format('YYYY-MM-DD')); | |
return existingArray; | |
} | |
var HolidayDisabledDates = []; | |
HolidayDisabledDates = addSlidingHolidayToArray(HolidayDisabledDates,'-01-01',3,'Monday','MLK'); | |
HolidayDisabledDates = addSlidingHolidayToArray(HolidayDisabledDates,'-02-01',3,'Monday','George Washington Birthday'); | |
HolidayDisabledDates = addSlidingHolidayToArray(HolidayDisabledDates,'-10-01',2,'Monday','Columbus Day'); | |
HolidayDisabledDates = addSlidingHolidayToArray(HolidayDisabledDates,'-11-01',4,'Thursday','Thanksgiving Day'); | |
var MemorialDayCurrent = getLastOccurringDayOfMonth(CurrentYear + '-05-01','Monday','Memorial Day'); | |
var LaborDayCurrent = getFirstOccurringDayOfMonth(CurrentYear + '-09-01','Monday'); | |
var MemorialDayNext = getLastOccurringDayOfMonth(NextYear + '-05-01','Monday'); | |
var LaborDayNext = getFirstOccurringDayOfMonth(NextYear + '-09-01','Monday'); | |
HolidayDisabledDates.push(MemorialDayCurrent); | |
HolidayDisabledDates.push(MemorialDayNext); | |
HolidayDisabledDates.push(LaborDayCurrent); | |
HolidayDisabledDates.push(LaborDayNext); | |
// associate this with any textbox that uses the .date-picker class | |
if ($('.date-picker')[0]) { | |
$('.date-picker').datetimepicker({ | |
format: 'YYYY-MM-DD', | |
minDate: moment().add(1,'days').format('YYYY-MM-DD'), | |
disabledDates: HolidayDisabledDates, | |
useCurrent:false | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment