Sabre Spark

Date helpers

General helpers for working with dates.

Option name Type Description
module helper/dates.js

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];
},

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];
},

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;
},

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];
},

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];
},

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 = [];
  }
},

getCurrentDate

method
 getCurrentDate() 

Get the current date.

Option name Type Description
return Object
getCurrentDate: function() {
  return this.dateToObject(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 {year: date.year + 1, month: date.month, day: date.day};
},

getNextWeek

method
 getNextWeek() 

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

getNextDay

method
 getNextDay() 

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

getNextMonth

method
 getNextMonth() 

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

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 {year: date.year - 1, month: date.month, day: date.day};
},

getPreviousWeek

method
 getPreviousWeek() 

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

getPreviousDay

method
 getPreviousDay() 

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

getPreviousMonth

method
 getPreviousMonth() 

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

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) {
  return this.dateToObject(new Date(date.year, date.month - 1, date.day - this.getDayOfWeek(date)));
},

getMonthEnd

method
 getMonthEnd() 

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

getDayOfWeek

method
 getDayOfWeek() 

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

dateToObject

method
 dateToObject() 

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