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;
},
Get a year.
get year() {
this._instanceCheck('year');
return this._date.getFullYear();
},
Option name | Type | Description |
---|---|---|
y | Number |
Set a year.
set year(y) {
this._instanceCheck('year');
this._clearCache();
return this._date.setFullYear(y);
},
Get a month.
get month() {
this._instanceCheck('month');
return this._date.getMonth() + 1;
},
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);
},
Get a day.
get day() {
this._instanceCheck('day');
return this._date.getDate();
},
Option name | Type | Description |
---|---|---|
d | Number | 1-31 |
Set a day.
set day(d) {
this._instanceCheck('day');
this._clearCache();
return this._date.setDate(d);
},
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;
},
Option name | Type | Description |
---|---|---|
num | Number | |
return | String |
Get the full name of the month.
getMonthName: function(num) {
return monthNames[num - 1];
},
Get the month name.
get monthName() {
this._instanceCheck('monthName');
return dateHelper.getMonthName(this.month);
},
Get the list of month names.
getMonthNames: function() {
return monthNames;
},
Option name | Type | Description |
---|---|---|
num | Number | |
return | String |
Get the short name of the month.
getMonthNameShort: function(num) {
return monthNamesShort[num - 1];
},
Get the month name.
get monthNameShort() {
this._instanceCheck('monthName');
return dateHelper.getMonthNameShort(this.month);
},
Get the list of short month names.
getMonthNamesShort: function() {
return monthNamesShort;
},
Option name | Type | Description |
---|---|---|
names | Array |
Set the month names.
setMonthNames: function(names) {
if (names.length === 12) monthNames = names;
},
Option name | Type | Description |
---|---|---|
names | Array |
Set the short month names.
setMonthNamesShort: function(names) {
if (names.length === 12) monthNamesShort = names;
},
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;
},
Get the day of the week.
get dayOfWeek() {
return dateHelper.getDayOfWeek(this._date);
},
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];
},
Get the day name.
get dayName() {
this._instanceCheck('dayName');
return dateHelper.getDayName(this.dayOfWeek);
},
Get the full name of the days of the week.
getDayNames: function() {
return adjustedDayNames.length ? adjustedDayNames : dayNames;
},
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];
},
Get the short day name.
get dayNameShort() {
this._instanceCheck('dayNameShort');
return dateHelper.getDayNameShort(this.dayOfWeek);
},
Get the full name of the days of the week.
getDayNamesShort: function() {
return adjustedDayNamesShort.length ? adjustedDayNamesShort : dayNamesShort;
},
Option name | Type | Description |
---|---|---|
names | Array |
Set the day names.
setDayNames: function(names) {
if (names.length === 7) dayNames = names;
},
Option name | Type | Description |
---|---|---|
names | Array |
Set the short day names.
setDayNamesShort: function(names) {
if (names.length === 7) dayNamesShort = names;
},
Get the index of the first day of the week.
getWeekStartsOn: function() {
return weekStartsOn;
},
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 = [];
}
},
Get the current date.
now: function() {
return dateHelper.create(new Date());
},
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));
},
Get the year following this.
get nextYear() {
this._instanceCheck('nextYear');
return this._cache.nextYear || (this._cache.nextYear = dateHelper.getNextYear(this));
},
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;
},
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));
},
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;
},
Get the start of the month for this date.
get monthStart() {
this._instanceCheck('monthStart');
return this._cache.monthStart || (this._cache.monthStart = dateHelper.getMonthStart(this));
},
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));
},
Get the week following this.
get nextWeek() {
this._instanceCheck('nextWeek');
return this._cache.nextWeek || (this._cache.nextWeek = dateHelper.getNextWeek(this));
},
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));
},
Get the day following this.
get nextDay() {
this._instanceCheck('nextDay');
return this._cache.nextDay || (this._cache.nextDay = dateHelper.getNextDay(this));
},
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));
},
Get the month following this.
get nextMonth() {
this._instanceCheck('nextMonth');
return this._cache.nextMonth || (this._cache.nextMonth = dateHelper.getNextMonth(this));
},
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));
},
Get the year preceding this.
get previousYear() {
this._instanceCheck('previousYear');
return this._cache.previousYear || (this._cache.previousYear = dateHelper.getPreviousYear(this));
},
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;
},
Get the week preceding this.
get previousWeek() {
this._instanceCheck('previousWeek');
return this._cache.previousWeek || (this._cache.previousWeek = dateHelper.getPreviousWeek(this));
},
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));
},
Get the day preceding this.
get previousDay() {
this._instanceCheck('previousDay');
return this._cache.previousDay || (this._cache.previousDay = dateHelper.getPreviousDay(this));
},
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));
},
Get the month preceding this.
get previousMonth() {
this._instanceCheck('previousMonth');
return this._cache.previousMonth || (this._cache.previousMonth = dateHelper.getPreviousMonth(this));
},
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));
},
Get the last day of the month.
get monthEnd() {
this._instanceCheck('monthEnd');
return this._cache.monthEnd || (this._cache.monthEnd = dateHelper.getMonthEnd(this));
},
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);
},
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;
},
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;
},
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;
},
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;
},
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);
},
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;
},
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;
},
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;
},
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;
},
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);
},
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;
},
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;
},
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;
},
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;
},
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;
},
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;
},
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()));
},
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];
},
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!');
},
Clear the cache.
_clearCache: function() {
this._instanceCheck('clearCache');
this._cache = {};
}
};
return dateHelper;
}));