create

method
 create() 

Option name Type Description
date Date
return Object

Transform a date into an object of date values.

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.

get year() {
  this._instanceCheck('year');
  return this._date.getFullYear();
},

year

property
 year 

Option name Type Description
y Number

Set a year.

set year(y) {
  this._instanceCheck('year');
  this._clearCache();
  return this._date.setFullYear(y);
},

month

property
 month 

Get a month.

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

month

property
 month 

Option name Type Description
m Number

1-12

Set a month.

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

day

property
 day 

Get a day.

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

day

property
 day 

Option name Type Description
d Number

1-31

Set a day.

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

set

method
 set() 

Option name Type Description
params Object

Sets the day, month and year values at once.

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() 

Option name Type Description
num Number
return String

Get the full name of the month.

getMonthName: function(num) {
  return monthNames[num - 1];
},

monthName

property
 monthName 

Get the month name.

get monthName() {
  this._instanceCheck('monthName');
  return dateHelper.getMonthName(this.month);
},

getMonthNames

method
 getMonthNames() 

Get the list of month names.

getMonthNames: function() {
  return monthNames;
},

getMonthNameShort

method
 getMonthNameShort() 

Option name Type Description
num Number
return String

Get the short name of the month.

getMonthNameShort: function(num) {
  return monthNamesShort[num - 1];
},

monthNameShort

property
 monthNameShort 

Get the month name.

get monthNameShort() {
  this._instanceCheck('monthName');
  return dateHelper.getMonthNameShort(this.month);
},

getMonthNamesShort

method
 getMonthNamesShort() 

Get the list of short month names.

getMonthNamesShort: function() {
  return monthNamesShort;
},

setMonthNames

method
 setMonthNames() 

Option name Type Description
names Array

Set the month names.

setMonthNames: function(names) {
  if (names.length === 12) monthNames = names;
},

setMonthNamesShort

method
 setMonthNamesShort() 

Option name Type Description
names Array

Set the short month names.

setMonthNamesShort: function(names) {
  if (names.length === 12) monthNamesShort = names;
},

getDayOfWeek

method
 getDayOfWeek() 

Option name Type Description
date Object
return Number

1-7

Get the day of the week for a given day.

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.

get dayOfWeek() {
  return dateHelper.getDayOfWeek(this._date);
},

getDayName

method
 getDayName() 

Option name Type Description
num Number
return String

Get the full name of a day of the week.

getDayName: function(num) {
  return dayNames[num - 1 + weekStartsOn] || dayNames[dayNames.length - num - 1 + weekStartsOn];
},

dayName

property
 dayName 

Get the day name.

get dayName() {
  this._instanceCheck('dayName');
  return dateHelper.getDayName(this.dayOfWeek);
},

getDayNames

method
 getDayNames() 

Get the full name of the days of the week.

getDayNames: function() {
  return adjustedDayNames.length ? adjustedDayNames : dayNames;
},

getDayNameShort

method
 getDayNameShort() 

Option name Type Description
num Number
return String

Get the short name of the day.

getDayNameShort: function(num) {
  return dayNamesShort[num - 1 + weekStartsOn] || dayNames[dayNames.length - num - 1 + weekStartsOn];
},

dayNameShort

property
 dayNameShort 

Get the short day name.

get dayNameShort() {
  this._instanceCheck('dayNameShort');
  return dateHelper.getDayNameShort(this.dayOfWeek);
},

getDayNamesShort

method
 getDayNamesShort() 

Get the full name of the days of the week.

getDayNamesShort: function() {
  return adjustedDayNamesShort.length ? adjustedDayNamesShort : dayNamesShort;
},

setDayNames

method
 setDayNames() 

Option name Type Description
names Array

Set the day names.

setDayNames: function(names) {
  if (names.length === 7) dayNames = names;
},

setDayNamesShort

method
 setDayNamesShort() 

Option name Type Description
names Array

Set the short day names.

setDayNamesShort: function(names) {
  if (names.length === 7) dayNamesShort = names;
},

getWeekStartsOn

method
 getWeekStartsOn() 

Get the index of the first day of the week.

getWeekStartsOn: function() {
  return weekStartsOn;
},

setWeekStartsOn

method
 setWeekStartsOn() 

Option name Type Description
index Number
return String

Set the index of the first day of the week.

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.

now: function() {
  return dateHelper.create(new Date());
},

getNextYear

method
 getNextYear() 

Option name Type Description
date Object
return Object

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.

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

nextYear

property
 nextYear 

Get the year following this.

get nextYear() {
  this._instanceCheck('nextYear');
  return this._cache.nextYear || (this._cache.nextYear = dateHelper.getNextYear(this));
},

getWeekStart

method
 getWeekStart() 

Option name Type Description
date Object
return Object

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

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.

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() 

Option name Type Description
date Object
return Object

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

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.

get monthStart() {
  this._instanceCheck('monthStart');
  return this._cache.monthStart || (this._cache.monthStart = dateHelper.getMonthStart(this));
},

getNextWeek

method
 getNextWeek() 

Option name Type Description
date Object
return Object

Get the next week after the given date.

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.

get nextWeek() {
  this._instanceCheck('nextWeek');
  return this._cache.nextWeek || (this._cache.nextWeek = dateHelper.getNextWeek(this));
},

getNextDay

method
 getNextDay() 

Option name Type Description
date Object
return Object

Get the next day after the given date.

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

nextDay

property
 nextDay 

Get the day following this.

get nextDay() {
  this._instanceCheck('nextDay');
  return this._cache.nextDay || (this._cache.nextDay = dateHelper.getNextDay(this));
},

getNextMonth

method
 getNextMonth() 

Option name Type Description
date Object
return Object

Get the next month after the given date.

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.

get nextMonth() {
  this._instanceCheck('nextMonth');
  return this._cache.nextMonth || (this._cache.nextMonth = dateHelper.getNextMonth(this));
},

getPreviousYear

method
 getPreviousYear() 

Option name Type Description
date Object
return Object

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.

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

previousYear

property
 previousYear 

Get the year preceding this.

get previousYear() {
  this._instanceCheck('previousYear');
  return this._cache.previousYear || (this._cache.previousYear = dateHelper.getPreviousYear(this));
},

getPreviousWeek

method
 getPreviousWeek() 

Option name Type Description
date Object
return Object

Get the previous week after the given date.

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.

get previousWeek() {
  this._instanceCheck('previousWeek');
  return this._cache.previousWeek || (this._cache.previousWeek = dateHelper.getPreviousWeek(this));
},

getPreviousDay

method
 getPreviousDay() 

Option name Type Description
date Object
return Object

Get the previous day after the given date.

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

previousDay

property
 previousDay 

Get the day preceding this.

get previousDay() {
  this._instanceCheck('previousDay');
  return this._cache.previousDay || (this._cache.previousDay = dateHelper.getPreviousDay(this));
},

getPreviousMonth

method
 getPreviousMonth() 

Option name Type Description
date Object
return Object

Get the previous month after the given date.

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

previousMonth

property
 previousMonth 

Get the month preceding this.

get previousMonth() {
  this._instanceCheck('previousMonth');
  return this._cache.previousMonth || (this._cache.previousMonth = dateHelper.getPreviousMonth(this));
},

getMonthEnd

method
 getMonthEnd() 

Option name Type Description
date Object
return Object

Get the last day of the month.

getMonthEnd: function(date) {
  return dateHelper.create(new Date(date.year, date.month, 0));
},

monthEnd

property
 monthEnd 

Get the last day of the month.

get monthEnd() {
  this._instanceCheck('monthEnd');
  return this._cache.monthEnd || (this._cache.monthEnd = dateHelper.getMonthEnd(this));
},

equal

method
 equal() 

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

Return a successful match only if all matches are found.

return Boolean

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

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

equalDay

method
 equalDay() 

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

Return a successful match only if all matches are found.

return Boolean

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

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a week equal to another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a month equal to another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a year equal to another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a date before another?

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

beforeDay

method
 beforeDay() 

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

Return a successful match only if all matches are found.

return Boolean

Is a given date before another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a week before another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a month before another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a year before another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a date after another?

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

afterDay

method
 afterDay() 

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

Return a successful match only if all matches are found.

return Boolean

Is a given date after another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a week after another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a month after another?

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() 

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

Return a successful match only if all matches are found.

return Boolean

Is a year after another?

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.

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.

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() 

Option name Type Description
date Object
return Object

Clone a date instance.

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() 

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

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

_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() 

Option name Type Description
prop String

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

_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;
}));