149 lines
3.7 KiB
CoffeeScript
149 lines
3.7 KiB
CoffeeScript
'use strict'
|
|
# taken from https://github.com/kalmecak/moment-business-days
|
|
#if (typeof require === 'function') {
|
|
# var moment = require('moment');
|
|
#}
|
|
|
|
moment.fn.isHoliday = ->
|
|
locale = @localeData()
|
|
if locale._holidays
|
|
if locale._holidays.indexOf(@format(locale._holidayFormat)) >= 0
|
|
return true
|
|
if locale.holiday
|
|
if locale.holiday(this)
|
|
return true
|
|
else
|
|
return false
|
|
false
|
|
|
|
moment.fn.isBusinessDay = (options) ->
|
|
locale = @localeData()
|
|
defaultWorkingWeekdays = [ 1, 2, 3, 4, 5 ]
|
|
workingWeekdays = options.business_days or locale._workingWeekdays or defaultWorkingWeekdays
|
|
if @isHoliday()
|
|
return false
|
|
if workingWeekdays.indexOf(@day()) >= 0
|
|
return true
|
|
false
|
|
|
|
moment.fn.businessDaysIntoMonth = ->
|
|
businessDay = if @isBusinessDay() then this else @prevBusinessDay()
|
|
monthBusinessDays = businessDay.monthBusinessDays()
|
|
businessDaysIntoMonth = undefined
|
|
monthBusinessDays.map (day, index) ->
|
|
if day.format('M/DD/YY') == businessDay.format('M/DD/YY')
|
|
businessDaysIntoMonth = index + 1
|
|
return
|
|
businessDaysIntoMonth
|
|
|
|
moment.fn.businessDiff = (param) ->
|
|
d1 = @clone()
|
|
d2 = param.clone()
|
|
start = if d1 < d2 then d1 else d2
|
|
end = if d2 > d1 then d2 else d1
|
|
daysBetween = 0
|
|
if start == end
|
|
return daysBetween
|
|
while start < end
|
|
if start.isBusinessDay()
|
|
daysBetween++
|
|
start.add 1, 'd'
|
|
daysBetween
|
|
|
|
moment.fn.businessAdd = (number, options = {}) ->
|
|
options.period ||= 'days'
|
|
day = @clone()
|
|
signal = if number < 0 then -1 else 1
|
|
remaining = Math.abs(number)
|
|
while remaining
|
|
day.add signal, options.period
|
|
if day.isBusinessDay(options)
|
|
remaining--
|
|
day
|
|
|
|
moment.fn.businessSubtract = (number, options = {}) ->
|
|
@businessAdd -number, options
|
|
|
|
moment.fn.nextBusinessDay = ->
|
|
iterator = 1
|
|
limit = 7
|
|
while iterator < limit
|
|
if @add(1, 'd').isBusinessDay()
|
|
break
|
|
iterator++
|
|
this
|
|
|
|
moment.fn.prevBusinessDay = ->
|
|
iterator = 1
|
|
limit = 7
|
|
while iterator < limit
|
|
if @subtract(1, 'd').isBusinessDay()
|
|
break
|
|
iterator++
|
|
this
|
|
|
|
moment.fn.monthBusinessDays = (partialEndDate) ->
|
|
me = @clone()
|
|
day = me.clone().startOf('month')
|
|
end = if partialEndDate then partialEndDate else me.clone().endOf('month')
|
|
daysArr = []
|
|
done = false
|
|
while !done
|
|
if day.isBusinessDay()
|
|
daysArr.push day.clone()
|
|
if end.diff(day.add(1, 'd')) < 0
|
|
done = true
|
|
daysArr
|
|
|
|
moment.fn.monthNaturalDays = (fromToday) ->
|
|
me = @clone()
|
|
day = if fromToday then me.clone() else me.clone().startOf('month')
|
|
end = me.clone().endOf('month')
|
|
daysArr = []
|
|
done = false
|
|
while !done
|
|
daysArr.push day.clone()
|
|
if end.diff(day.add(1, 'd')) < 0
|
|
done = true
|
|
daysArr
|
|
|
|
moment.fn.monthBusinessWeeks = (fromToday) ->
|
|
me = @clone()
|
|
day = if fromToday then me.clone() else me.clone().startOf('month')
|
|
end = me.clone().endOf('month')
|
|
weeksArr = []
|
|
daysArr = []
|
|
done = false
|
|
while !done
|
|
if day.day() >= 1 and day.day() < 6
|
|
daysArr.push day.clone()
|
|
if day.day() == 5
|
|
weeksArr.push daysArr
|
|
daysArr = []
|
|
if end.diff(day.add(1, 'd')) < 0
|
|
if daysArr.length < 5
|
|
weeksArr.push daysArr
|
|
done = true
|
|
weeksArr
|
|
|
|
moment.fn.monthNaturalWeeks = (fromToday) ->
|
|
me = @clone()
|
|
day = if fromToday then me.clone() else me.clone().startOf('month')
|
|
end = me.clone().endOf('month')
|
|
weeksArr = []
|
|
daysArr = []
|
|
done = false
|
|
while !done
|
|
daysArr.push day.clone()
|
|
if day.day() == 6
|
|
weeksArr.push daysArr
|
|
daysArr = []
|
|
if end.diff(day.add(1, 'd')) < 0
|
|
if daysArr.length < 7
|
|
weeksArr.push daysArr
|
|
done = true
|
|
weeksArr
|
|
|
|
if typeof module isnt 'undefined' and module.exports
|
|
module.exports = moment
|