Sabre Spark

Date helper

General helpers for working with dates.

Option name Type Description
module helper/date.js

create

method
 create() 

Transform a date into an object of date values.

Option name Type Description
date Date
return Object
create: function(date) {

  date = date instanceof Date ? date : new Date(date.year, date.month - 1, date.day);

  var inst = Object.create(dateHelper);
  inst._date = date;
  inst._cache = {};

  return inst;
},

year

property
 year 

Get a year.

Option name Type Description
return Number
get year() {
  this._instanceCheck('year');
  return this._date.getFullYear();
},

year

property
 year 

Set a year.

Option name Type Description
y Number
set year(y) {
  this._instanceCheck('year');
  this._clearCache();
  return this._date.setFullYear(y);
},

month

property
 month 

Get a month.

Option name Type Description
return Number

1-12

get month() {
  this._instanceCheck('month');
  return this._date.getMonth() + 1;
},

month

property
 month 

Set a month.

Option name Type Description
m Number

1-12

set month(m) {
  this._instanceCheck('month');
  this._clearCache();
  return this._date.setMonth(m - 1);
},

day

property
 day 

Get a day.

Option name Type Description
return Number

1-31

get day() {
  this._instanceCheck('day');
  return this._date.getDate();
},

day

property
 day 

Set a day.

Option name Type Description
d Number

1-31

set day(d) {
  this._instanceCheck('day');
  this._clearCache();
  return this._date.setDate(d);
},

set

method
 set() 

Sets the day, month and year values at once.

Option name Type Description
params Object
set: function(params) {
  params = params || {};
  this.year = params.year || this.year;
  this.month = params.month || this.month;
  this.day = params.day || this.day;
},

getMonthName

method
 getMonthName() 

Get the full name of the month.

Option name Type Description
num Number
return String
getMonthName: function(num) {
  return monthNames[num - 1];
},

monthName

property
 monthName 

Get the month name.

Option name Type Description
return String
get monthName() {
  this._instanceCheck('monthName');
  return dateHelper.getMonthName(this.month);
},

getMonthNames

method
 getMonthNames() 

Get the list of month names.

Option name Type Description
return Array
getMonthNames: function() {
  return monthNames;
},

getMonthNameShort

method
 getMonthNameShort() 

Get the short name of the month.

Option name Type Description
num Number
return String
getMonthNameShort: function(num) {
  return monthNamesShort[num - 1];
},

monthNameShort

property
 monthNameShort 

Get the month name.

Option name Type Description
return String
get monthNameShort() {
  this._instanceCheck('monthName');
  return dateHelper.getMonthNameShort(this.month);
},

getMonthNamesShort

method
 getMonthNamesShort() 

Get the list of short month names.

Option name Type Description
return Array
getMonthNamesShort: function() {
  return monthNamesShort;
},

setMonthNames

method
 setMonthNames() 

Set the month names.

Option name Type Description
names Array
setMonthNames: function(names) {
  if (names.length === 12) monthNames = names;
},

setMonthNamesShort

method
 setMonthNamesShort() 

Set the short month names.

Option name Type Description
names Array
setMonthNamesShort: function(names) {
  if (names.length === 12) monthNamesShort = names;
},

getDayOfWeek

method
 getDayOfWeek() 

Get the day of the week for a given day.

Option name Type Description
date Object
return Number

1-7

getDayOfWeek: function(date) {
  var day = (date instanceof Date ? date : new Date(date.year, date.month - 1, date.day)).getDay() - weekStartsOn;
  return (day < 0 ? 7 - Math.abs(day) : day) + 1;
},

dayOfWeek

property
 dayOfWeek 

Get the day of the week.

Option name Type Description
return Number
get dayOfWeek() {
  return dateHelper.getDayOfWeek(this._date);
},

getDayName

method
 getDayName() 

Get the full name of a day of the week.

Option name Type Description
num Number
return String
getDayName: function(num) {
  return dayNames[num - 1 + weekStartsOn] || dayNames[dayNames.length - num - 1 + weekStartsOn];
},

dayName

property
 dayName 

Get the day name.

Option name Type Description
return String
get dayName() {
  this._instanceCheck('dayName');
  return dateHelper.getDayName(this.dayOfWeek);
},

getDayNames

method
 getDayNames() 

Get the full name of the days of the week.

Option name Type Description
return Array
getDayNames: function() {
  return adjustedDayNames.length ? adjustedDayNames : dayNames;
},

getDayNameShort

method
 getDayNameShort() 

Get the short name of the day.

Option name Type Description
num Number
return String
getDayNameShort: function(num) {
  return dayNamesShort[num - 1 + weekStartsOn] || dayNames[dayNames.length - num - 1 + weekStartsOn];
},

dayNameShort

property
 dayNameShort 

Get the short day name.

Option name Type Description
return String
get dayNameShort() {
  this._instanceCheck('dayNameShort');
  return dateHelper.getDayNameShort(this.dayOfWeek);
},

getDayNamesShort

method
 getDayNamesShort() 

Get the full name of the days of the week.

Option name Type Description
return Array
getDayNamesShort: function() {
  return adjustedDayNamesShort.length ? adjustedDayNamesShort : dayNamesShort;
},

setDayNames

method
 setDayNames() 

Set the day names.

Option name Type Description
names Array
setDayNames: function(names) {
  if (names.length === 7) dayNames = names;
},

setDayNamesShort

method
 setDayNamesShort() 

Set the short day names.

Option name Type Description
names Array
setDayNamesShort: function(names) {
  if (names.length === 7) dayNamesShort = names;
},

getWeekStartsOn

method
 getWeekStartsOn() 

Get the index of the first day of the week.

Option name Type Description
return Number
getWeekStartsOn: function() {
  return weekStartsOn;
},

setWeekStartsOn

method
 setWeekStartsOn() 

Set the index of the first day of the week.

Option name Type Description
index Number
return String
setWeekStartsOn: function(number) {

  weekStartsOn = number;

  if (number) {
    adjustedDayNames = dayNames.slice(weekStartsOn);
    adjustedDayNames = adjustedDayNames.concat(dayNames.slice(0, weekStartsOn));
    adjustedDayNamesShort = dayNamesShort.slice(weekStartsOn);
    adjustedDayNamesShort = adjustedDayNamesShort.concat(dayNamesShort.slice(0, weekStartsOn));
  } else {
    adjustedDayNames = [];
    adjustedDayNamesShort = [];
  }
},

now

method
 now() 

Get the current date.

Option name Type Description
return Object
now: function() {
  return dateHelper.create(new Date());
},

getNextYear

method
 getNextYear() 

Get the next year after the given date.
This obviously isn't very complicated, but it exists
for parity with how we get the week, day and month.

Option name Type Description
date Object
return Object
getNextYear: function(date) {
  return dateHelper.create(new Date(date.year + 1, date.month - 1, date.day));
},

nextYear

property
 nextYear 

Get the year following this.

Option name Type Description
return Object
get nextYear() {
  this._instanceCheck('nextYear');
  return this._cache.nextYear || (this._cache.nextYear = dateHelper.getNextYear(this));
},

getWeekStart

method
 getWeekStart() 

Get the first day of the week for a given date.

Option name Type Description
date Object
return Object
getWeekStart: function(date) {
  var inst = dateHelper.create(new Date(date.year, date.month - 1, date.day - dateHelper.getDayOfWeek(date) + 1));
  inst.weekStartsOn = weekStartsOn;
  return inst;
},

weekStart

property
 weekStart 

Get the start of the week for this date.

Option name Type Description
return Object
get weekStart() {
  this._instanceCheck('weekStart');
  return this._cache.weekStart && this._cache.weekStart.weekStartsOn === weekStartsOn ? this._cache.weekStart : (this._cache.weekStart = dateHelper.getWeekStart(this));
},

getMonthStart

method
 getMonthStart() 

Get the first day of the month for a given date.

Option name Type Description
date Object
return Object
getMonthStart: function(date) {
  var inst = dateHelper.create(new Date(date.year, date.month - 1, 1));
  return inst;
},

monthStart

property
 monthStart 

Get the start of the month for this date.

Option name Type Description
return Object
get monthStart() {
  this._instanceCheck('monthStart');
  return this._cache.monthStart || (this._cache.monthStart = dateHelper.getMonthStart(this));
},

getNextWeek

method
 getNextWeek() 

Get the next week after the given date.

Option name Type Description
date Object
return Object
getNextWeek: function(date) {
  var start = dateHelper.getWeekStart(date);
  return dateHelper.create(new Date(start.year, start.month - 1, start.day + 7));
},

nextWeek

property
 nextWeek 

Get the week following this.

Option name Type Description
return Object
get nextWeek() {
  this._instanceCheck('nextWeek');
  return this._cache.nextWeek || (this._cache.nextWeek = dateHelper.getNextWeek(this));
},

getNextDay

method
 getNextDay() 

Get the next day after the given date.

Option name Type Description
date Object
return Object
getNextDay: function(date) {
  return dateHelper.create(new Date(date.year, date.month - 1, date.day + 1));
},

nextDay

property
 nextDay 

Get the day following this.

Option name Type Description
return Object
get nextDay() {
  this._instanceCheck('nextDay');
  return this._cache.nextDay || (this._cache.nextDay = dateHelper.getNextDay(this));
},

getNextMonth

method
 getNextMonth() 

Get the next month after the given date.

Option name Type Description
date Object
return Object
getNextMonth: function(date) {
  // Date() has a bug if last day of month is 31 when calculating the next month.
  // Need to account for that so that it doesn't round up the date/month.
  return date.day === 31 ? dateHelper.create(new Date(date.year, date.month, 1)) : dateHelper.create(new Date(date.year, date.month, date.day));
},

nextMonth

property
 nextMonth 

Get the month following this.

Option name Type Description
return Object
get nextMonth() {
  this._instanceCheck('nextMonth');
  return this._cache.nextMonth || (this._cache.nextMonth = dateHelper.getNextMonth(this));
},

getPreviousYear

method
 getPreviousYear() 

Get the previous year after the given date.
This obviously isn't very complicated, but it exists
for parity with how we get the week, day and month.

Option name Type Description
date Object
return Object
getPreviousYear: function(date) {
  return dateHelper.create(new Date(date.year - 1, date.month - 1, date.day));
},

previousYear

property
 previousYear 

Get the year preceding this.

Option name Type Description
return Object
get previousYear() {
  this._instanceCheck('previousYear');
  return this._cache.previousYear || (this._cache.previousYear = dateHelper.getPreviousYear(this));
},

getPreviousWeek

method
 getPreviousWeek() 

Get the previous week after the given date.

Option name Type Description
date Object
return Object
getPreviousWeek: function(date) {
  var start = dateHelper.getWeekStart(date);
  var inst = dateHelper.create(new Date(start.year, start.month - 1, start.day - 7));
  inst.weekStartsOn = weekStartsOn;
  return inst;
},

previousWeek

property
 previousWeek 

Get the week preceding this.

Option name Type Description
return Object
get previousWeek() {
  this._instanceCheck('previousWeek');
  return this._cache.previousWeek || (this._cache.previousWeek = dateHelper.getPreviousWeek(this));
},

getPreviousDay

method
 getPreviousDay() 

Get the previous day after the given date.

Option name Type Description
date Object
return Object
getPreviousDay: function(date) {
  return dateHelper.create(new Date(date.year, date.month - 1, date.day - 1));
},

previousDay

property
 previousDay 

Get the day preceding this.

Option name Type Description
return Object
get previousDay() {
  this._instanceCheck('previousDay');
  return this._cache.previousDay || (this._cache.previousDay = dateHelper.getPreviousDay(this));
},

getPreviousMonth

method
 getPreviousMonth() 

Get the previous month after the given date.

Option name Type Description
date Object
return Object
getPreviousMonth: function(date) {
  return dateHelper.create(new Date(date.year, date.month - 2, date.day));
},

previousMonth

property
 previousMonth 

Get the month preceding this.

Option name Type Description
return Object
get previousMonth() {
  this._instanceCheck('previousMonth');
  return this._cache.previousMonth || (this._cache.previousMonth = dateHelper.getPreviousMonth(this));
},

getMonthEnd

method
 getMonthEnd() 

Get the last day of the month.

Option name Type Description
date Object
return Object
getMonthEnd: function(date) {
  return dateHelper.create(new Date(date.year, date.month, 0));
},

monthEnd

property
 monthEnd 

Get the last day of the month.

Option name Type Description
return Object
get monthEnd() {
  this._instanceCheck('monthEnd');
  return this._cache.monthEnd || (this._cache.monthEnd = dateHelper.getMonthEnd(this));
},

equal

method
 equal() 

Does a given day equal another? Or is it present in a list of others?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
equal: function(date, compare, full) {
  return this.equalDay(date, compare, full);
},

equalDay

method
 equalDay() 

Does a given day equal another? Or is it present in a list of others?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
equalDay: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (compare[i] && date.year === compare[i].year && date.month === compare[i].month && date.day === compare[i].day)
      matches++;
  }

  return full ? matches === len : !!matches;
},

equalWeek

method
 equalWeek() 

Is a week equal to another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
equalWeek: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || !date.weekStart.equalDay(compare[i].weekStart))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

equalMonth

method
 equalMonth() 

Is a month equal to another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
equalMonth: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || (date.year !== compare[i].year) || (date.year === compare[i].year && date.month !== compare[i].month))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

equalYear

method
 equalYear() 

Is a year equal to another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
equalYear: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || date.year !== compare[i].year)
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

before

method
 before() 

Is a date before another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
before: function(date, compare, full) {
  return this.beforeDay(date, compare, full);
},

beforeDay

method
 beforeDay() 

Is a given date before another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
beforeDay: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || date._date >= compare[i]._date)
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

beforeWeek

method
 beforeWeek() 

Is a week before another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
beforeWeek: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || !date.weekStart.beforeDay(compare[i].weekStart))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

beforeMonth

method
 beforeMonth() 

Is a month before another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
beforeMonth: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || (date.year > compare[i].year) || (date.year === compare[i].year && date.month >= compare[i].month))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

beforeYear

method
 beforeYear() 

Is a year before another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
beforeYear: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || date.year >= compare[i].year)
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

after

method
 after() 

Is a date after another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
after: function(date, compare, full) {
  return this.afterDay(date, compare, full);
},

afterDay

method
 afterDay() 

Is a given date after another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
afterDay: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || date._date <= compare[i]._date)
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

afterWeek

method
 afterWeek() 

Is a week after another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
afterWeek: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || !date.weekStart.afterDay(compare[i].weekStart))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

afterMonth

method
 afterMonth() 

Is a month after another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
afterMonth: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || (date.year < compare[i].year) || (date.year === compare[i].year && date.month <= compare[i].month))
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

afterYear

method
 afterYear() 

Is a year after another?

Option name Type Description
date Object
compare Object, Array
full Boolean

Return a successful match only if all matches are found.

return Boolean
afterYear: function(date, compare, full) {

  var args = this._checkComparisonArgs(date, compare, full);
  date = args[0];
  compare = args[1];
  full = args[2];

  var i = 0;
  var len = compare.length;
  var matches = 0;

  for (; i < len; i++) {
    if (!compare[i] || date.year <= compare[i].year)
      continue;
    else
      matches++;
  }

  return full ? matches === len : !!matches;
},

earliest

method
 earliest() 

Get the earliest date in an array.

Option name Type Description
return Object
earliest: function(arr) {

  var i = 0;
  var len = arr.length;
  var e;

  for (; i < len; i++) {
    if (!e || arr[i].before(e))
      e = arr[i];
  }

  return e;
},

latest

method
 latest() 

Get the latest date in an array.

Option name Type Description
return Object
latest: function(arr) {

  var i = 0;
  var len = arr.length;
  var l;

  for (; i < len; i++) {
    if (!l || arr[i].after(l))
      l = arr[i];
  }

  return l;
},

clone

method
 clone() 

Clone a date instance.

Option name Type Description
date Object
return Object
clone: function(date) {

  // If we weren't passed a date, use this instance.
  if (!date && this._date && this._date instanceof Date && dateHelper.isPrototypeOf(this)) {
    date = this;
  }

  // No date, can't clone.
  if (!date) {
    throw new Error('Must pass a date to clone or call on an instance.');
  }

  return dateHelper.create(new Date(date._date.valueOf()));
},

_checkComparisonArgs

method
 _checkComparisonArgs() 

If a comparison function is called on an instance, properly
assign the vars.

Option name Type Description
date Object
compare Object, Array
full Boolean
_checkComparisonArgs: function(date, compare, full) {

  if (compare === undefined || typeof compare === 'boolean') {

    if (!dateHelper.isPrototypeOf(this)) {
      throw new Error('Cannot compare only one date!');
    }

    full = compare;
    compare = date;
    date = this;
  }

  compare = compare instanceof Array ? compare : [compare];

  return [date, compare, full];
},

_instanceCheck

method
 _instanceCheck() 

Check to see if we have an instance of the date object.

Option name Type Description
prop String
_instanceCheck: function(prop) {
  if (!this._date || !(this._date instanceof Date) || !dateHelper.isPrototypeOf(this))
    throw new Error('Cannot access the property "' + prop + '" of the date helper with creating an instance!');
},

_clearCache

method
 _clearCache() 

Clear the cache.

_clearCache: function() {
  this._instanceCheck('clearCache');
  this._cache = {};
}
  };

  return dateHelper;
}));