General helpers for working with dates.
Option name | Type | Description |
---|---|---|
module | helper/dates.js |
Get the full name of the month.
Option name | Type | Description |
---|---|---|
num | Number | |
return | String |
getMonthName: function(num) {
return monthNames[num - 1];
},
Get the list of month names.
Option name | Type | Description |
---|---|---|
return | Array |
getMonthNames: function() {
return monthNames;
},
Get the short name of the month.
Option name | Type | Description |
---|---|---|
num | Number | |
return | String |
getMonthNameShort: function(num) {
return monthNamesShort[num - 1];
},
Get the list of short month names.
Option name | Type | Description |
---|---|---|
return | Array |
getMonthNamesShort: function() {
return monthNamesShort;
},
Set the month names.
Option name | Type | Description |
---|---|---|
names | Array |
setMonthNames: function(names) {
if (names.length === 12) monthNames = names;
},
Set the short month names.
Option name | Type | Description |
---|---|---|
names | Array |
setMonthNamesShort: function(names) {
if (names.length === 12) monthNamesShort = names;
},
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];
},
Get the full name of the days of the week.
Option name | Type | Description |
---|---|---|
return | Array |
getDayNames: function() {
return adjustedDayNames.length ? adjustedDayNames : dayNames;
},
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];
},
Get the full name of the days of the week.
Option name | Type | Description |
---|---|---|
return | Array |
getDayNamesShort: function() {
return adjustedDayNamesShort.length ? adjustedDayNamesShort : dayNamesShort;
},
Set the day names.
Option name | Type | Description |
---|---|---|
names | Array |
setDayNames: function(names) {
if (names.length === 7) dayNames = names;
},
Set the short day names.
Option name | Type | Description |
---|---|---|
names | Array |
setDayNamesShort: function(names) {
if (names.length === 7) dayNamesShort = names;
},
Get the index of the first day of the week.
Option name | Type | Description |
---|---|---|
return | Number |
getWeekStartsOn: function() {
return weekStartsOn;
},
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 = [];
}
},
Get the current date.
Option name | Type | Description |
---|---|---|
return | Object |
getCurrentDate: function() {
return this.dateToObject(new Date());
},
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 {year: date.year + 1, month: date.month, day: date.day};
},
Get the next week after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getNextWeek: function(date) {
var start = this.getWeekStart(date);
return this.dateToObject(new Date(start.year, start.month - 1, start.day + 7));
},
Get the next day after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getNextDay: function(date) {
return this.dateToObject(new Date(date.year, date.month - 1, date.day + 1));
},
Get the next month after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getNextMonth: function(date) {
return this.dateToObject(new Date(date.year, date.month, date.day));
},
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 {year: date.year - 1, month: date.month, day: date.day};
},
Get the previous week after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getPreviousWeek: function(date) {
var start = this.getWeekStart(date);
return this.dateToObject(new Date(start.year, start.month - 1, start.day - 7));
},
Get the previous day after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getPreviousDay: function(date) {
return this.dateToObject(new Date(date.year, date.month - 1, date.day - 1));
},
Get the previous month after the given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getPreviousMonth: function(date) {
return this.dateToObject(new Date(date.year, date.month - 2, date.day));
},
Get the first day of the week for a given date.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getWeekStart: function(date) {
return this.dateToObject(new Date(date.year, date.month - 1, date.day - this.getDayOfWeek(date)));
},
Get the last day of the month.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Object |
getMonthEnd: function(date) {
return this.dateToObject(new Date(date.year, date.month, 0));
},
Get the day of the week for a given day.
Option name | Type | Description |
---|---|---|
date | Object | |
return | Number |
getDayOfWeek: function(date) {
var day = new Date(date.year, date.month - 1, date.day).getUTCDay() - weekStartsOn;
return (day < 0 ? 7 - Math.abs(day) : day);
},
Transform a date into an object of date values.
Option name | Type | Description |
---|---|---|
date | Date | |
return | Object |
dateToObject: function(date) {
return {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate()
};
}
};
}));