tempusjs
Version:
Easy and fast datetime library.
1,427 lines (1,402 loc) • 93.2 kB
JavaScript
/**
* @doc module
* @name tempus
* @author Aleksey Kuznetsov, me@akuzn.com
* @version 0.2.18
* @url https://github.com/crusat/tempus-js
* @description
* Library with date/time methods.
*/
(function (undefined) {
"use strict";
// *************************************************
// * *
// * COMPATIBILITY *
// * *
// *************************************************
// fix Array.indexOf for old browsers
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (obj, start) {
var i, j;
for (i = (start || 0), j = this.length; i < j; i += 1) {
if (this[i] === obj) {
return i;
}
}
return -1;
};
}
// *************************************************
// * *
// * CORE *
// * *
// *************************************************
var root = this,
formattingWithNulls = function (val, symb_count) {
var v = val.toString();
while (v.length < symb_count) {
v = '0' + v;
}
return v;
},
parseBadFormat = function (date, defaults) {
if (defaults !== undefined) {
date.year(defaults.year() || defaults.year);
date.month(defaults.month() || defaults.month);
date.day(defaults.day() || defaults.day);
date.hours(defaults.hours() || defaults.hours);
date.minutes(defaults.minutes() || defaults.minutes);
date.seconds(defaults.seconds() || defaults.seconds);
date.milliseconds(defaults.milliseconds() || defaults.milliseconds);
return date;
}
return undefined;
},
detectTimeFormat = function (str, startFrom) {
var tmpChars, format = '';
tmpChars = str.slice(startFrom, startFrom + 1);
if (tmpChars !== '' && !isNaN(Number(tmpChars))) {
format += '%H';
}
tmpChars = str.charAt(startFrom + 2);
if (tmpChars !== '' && tmpChars === ':') {
format += tmpChars;
}
tmpChars = str.slice(startFrom + 3, startFrom + 4);
if (tmpChars !== '' && !isNaN(Number(tmpChars))) {
format += '%M';
}
tmpChars = str.charAt(startFrom + 5);
if (tmpChars !== '' && tmpChars === ':') {
format += tmpChars;
}
tmpChars = str.slice(startFrom + 6, startFrom + 7);
if (tmpChars !== '' && !isNaN(Number(tmpChars))) {
format += '%S';
}
return format;
},
detectDateFormat = function (str, startFrom) {
var format,
part1 = [
str.slice(startFrom, startFrom + 1),
str.charAt(startFrom + 2),
str.slice(startFrom + 3, startFrom + 4),
str.charAt(startFrom + 5),
str.slice(startFrom + 6, startFrom + 9)
],
part2;
if (!isNaN(Number(part1[0])) && !isNaN(Number(part1[2])) && !isNaN(Number(part1[4]))) {
if (part1[1] === '.' && part1[3] === '.') {
format = '%d.%m.%Y';
} else if (part1[1] === '-' && part1[3] === '-') {
format = '%m-%d-%Y';
} else if (part1[1] === '/' && part1[3] === '/') {
format = '%m/%d/%Y';
}
return format;
}
part2 = [
str.slice(startFrom, startFrom + 3),
str.charAt(startFrom + 4),
str.slice(startFrom + 5, startFrom + 6),
str.charAt(startFrom + 7),
str.slice(startFrom + 8, startFrom + 9)
];
if (!isNaN(Number(part2[0])) && !isNaN(Number(part2[2])) && !isNaN(Number(part2[4]))) {
if (part2[1] === '-' && part2[3] === '-') {
format = '%Y-%m-%d';
}
return format;
}
return '';
},
oldTempus = root.tempus,
tempus,
classes,
nav = root.navigator || {},
lang = 'en',
translations = {
"en": {
"JANUARY_SHORT": "Jan",
"FEBRUARY_SHORT": "Feb",
"MARCH_SHORT": "Mar",
"APRIL_SHORT": "Apr",
"MAY_SHORT": "May",
"JUNE_SHORT": "Jun",
"JULY_SHORT": "Jul",
"AUGUST_SHORT": "Aug",
"SEPTEMBER_SHORT": "Sep",
"OCTOBER_SHORT": "Oct",
"NOVEMBER_SHORT": "Nov",
"DECEMBER_SHORT": "Dec",
"JANUARY_LONG": "January",
"FEBRUARY_LONG": "February",
"MARCH_LONG": "March",
"APRIL_LONG": "April",
"MAY_LONG": "May",
"JUNE_LONG": "June",
"JULY_LONG": "July",
"AUGUST_LONG": "August",
"SEPTEMBER_LONG": "September",
"OCTOBER_LONG": "October",
"NOVEMBER_LONG": "November",
"DECEMBER_LONG": "December",
"SUNDAY_SHORT": "Sun",
"MONDAY_SHORT": "Mon",
"TUESDAY_SHORT": "Tue",
"WEDNESDAY_SHORT": "Wed",
"THURSDAY_SHORT": "Thu",
"FRIDAY_SHORT": "Fri",
"SATURDAY_SHORT": "Sat",
"SUNDAY_LONG": "Sunday",
"MONDAY_LONG": "Monday",
"TUESDAY_LONG": "Tuesday",
"WEDNESDAY_LONG": "Wednesday",
"THURSDAY_LONG": "Thursday",
"FRIDAY_LONG": "Friday",
"SATURDAY_LONG": "Saturday"
},
"ru": {
"JANUARY_SHORT": "Янв",
"FEBRUARY_SHORT": "Фев",
"MARCH_SHORT": "Мар",
"APRIL_SHORT": "Апр",
"MAY_SHORT": "Май",
"JUNE_SHORT": "Июн",
"JULY_SHORT": "Июл",
"AUGUST_SHORT": "Авг",
"SEPTEMBER_SHORT": "Сен",
"OCTOBER_SHORT": "Окт",
"NOVEMBER_SHORT": "Ноя",
"DECEMBER_SHORT": "Дек",
"JANUARY_LONG": "Январь",
"FEBRUARY_LONG": "Февраль",
"MARCH_LONG": "Март",
"APRIL_LONG": "Апрель",
"MAY_LONG": "Май",
"JUNE_LONG": "Июнь",
"JULY_LONG": "Июль",
"AUGUST_LONG": "Август",
"SEPTEMBER_LONG": "Сентябрь",
"OCTOBER_LONG": "Октябрь",
"NOVEMBER_LONG": "Ноябрь",
"DECEMBER_LONG": "Декабрь",
"SUNDAY_SHORT": "Вс",
"MONDAY_SHORT": "Пн",
"TUESDAY_SHORT": "Вт",
"WEDNESDAY_SHORT": "Ср",
"THURSDAY_SHORT": "Чт",
"FRIDAY_SHORT": "Пт",
"SATURDAY_SHORT": "Сб",
"SUNDAY_LONG": "Воскресенье",
"MONDAY_LONG": "Понедельник",
"TUESDAY_LONG": "Вторник",
"WEDNESDAY_LONG": "Среда",
"THURSDAY_LONG": "Четверг",
"FRIDAY_LONG": "Пятница",
"SATURDAY_LONG": "Суббота"
},
"ua": {
"JANUARY_SHORT": "Січ",
"FEBRUARY_SHORT": "Лют",
"MARCH_SHORT": "Берез",
"APRIL_SHORT": "Квіт",
"MAY_SHORT": "Трав",
"JUNE_SHORT": "Черв",
"JULY_SHORT": "Лип",
"AUGUST_SHORT": "Серп",
"SEPTEMBER_SHORT": "Верес",
"OCTOBER_SHORT": "Жовт",
"NOVEMBER_SHORT": "Листоп",
"DECEMBER_SHORT": "Груд",
"JANUARY_LONG": "Січень",
"FEBRUARY_LONG": "Лютий",
"MARCH_LONG": "Березень",
"APRIL_LONG": "Квітень",
"MAY_LONG": "Травень",
"JUNE_LONG": "Червень",
"JULY_LONG": "Липень",
"AUGUST_LONG": "Серпень",
"SEPTEMBER_LONG": "Вересень",
"OCTOBER_LONG": "Жовтень",
"NOVEMBER_LONG": "Листопад",
"DECEMBER_LONG": "Грудень",
"SUNDAY_SHORT": "Нд",
"MONDAY_SHORT": "Пн",
"TUESDAY_SHORT": "Вт",
"WEDNESDAY_SHORT": "Ср",
"THURSDAY_SHORT": "Чт",
"FRIDAY_SHORT": "Пт",
"SATURDAY_SHORT": "Сб",
"SUNDAY_LONG": "Неділя",
"MONDAY_LONG": "Понеділок",
"TUESDAY_LONG": "Вівторок",
"WEDNESDAY_LONG": "Середа",
"THURSDAY_LONG": "Четвер",
"FRIDAY_LONG": "П’ятниця",
"SATURDAY_LONG": "Субота"
},
"de": {
"JANUARY_SHORT": "Jan",
"FEBRUARY_SHORT": "Feb",
"MARCH_SHORT": "März",
"APRIL_SHORT": "Apr",
"MAY_SHORT": "Mai",
"JUNE_SHORT": "Juni",
"JULY_SHORT": "Juli",
"AUGUST_SHORT": "Aug",
"SEPTEMBER_SHORT": "Sept",
"OCTOBER_SHORT": "Okt",
"NOVEMBER_SHORT": "Nov",
"DECEMBER_SHORT": "Dez",
"JANUARY_LONG": "Januar",
"FEBRUARY_LONG": "Februar",
"MARCH_LONG": "März",
"APRIL_LONG": "April",
"MAY_LONG": "Mai",
"JUNE_LONG": "Juni",
"JULY_LONG": "Juli",
"AUGUST_LONG": "August",
"SEPTEMBER_LONG": "September",
"OCTOBER_LONG": "Oktober",
"NOVEMBER_LONG": "November",
"DECEMBER_LONG": "Dezember",
"SUNDAY_SHORT": "So",
"MONDAY_SHORT": "Mo",
"TUESDAY_SHORT": "Di",
"WEDNESDAY_SHORT": "Mi",
"THURSDAY_SHORT": "Do",
"FRIDAY_SHORT": "Fr",
"SATURDAY_SHORT": "Sa",
"SUNDAY_LONG": "Sonntag",
"MONDAY_LONG": "Montag",
"TUESDAY_LONG": "Dienstag",
"WEDNESDAY_LONG": "Mittwoch",
"THURSDAY_LONG": "Donnerstag",
"FRIDAY_LONG": "Freitag",
"SATURDAY_LONG": "Samstag"
}
},
getMonthLongNames = function () {
return [
translations[lang].JANUARY_LONG,
translations[lang].FEBRUARY_LONG,
translations[lang].MARCH_LONG,
translations[lang].APRIL_LONG,
translations[lang].MAY_LONG,
translations[lang].JUNE_LONG,
translations[lang].JULY_LONG,
translations[lang].AUGUST_LONG,
translations[lang].SEPTEMBER_LONG,
translations[lang].OCTOBER_LONG,
translations[lang].NOVEMBER_LONG,
translations[lang].DECEMBER_LONG
];
},
getMonthShortNames = function() {
return [
translations[lang].JANUARY_SHORT,
translations[lang].FEBRUARY_SHORT,
translations[lang].MARCH_SHORT,
translations[lang].APRIL_SHORT,
translations[lang].MAY_SHORT,
translations[lang].JUNE_SHORT,
translations[lang].JULY_SHORT,
translations[lang].AUGUST_SHORT,
translations[lang].SEPTEMBER_SHORT,
translations[lang].OCTOBER_SHORT,
translations[lang].NOVEMBER_SHORT,
translations[lang].DECEMBER_SHORT
];
},
getDayLongNames = function () {
return [
translations[lang].SUNDAY_LONG,
translations[lang].MONDAY_LONG,
translations[lang].TUESDAY_LONG,
translations[lang].WEDNESDAY_LONG,
translations[lang].THURSDAY_LONG,
translations[lang].FRIDAY_LONG,
translations[lang].SATURDAY_LONG
];
},
getDayShortNames = function () {
return [
translations[lang].SUNDAY_SHORT,
translations[lang].MONDAY_SHORT,
translations[lang].TUESDAY_SHORT,
translations[lang].WEDNESDAY_SHORT,
translations[lang].THURSDAY_SHORT,
translations[lang].FRIDAY_SHORT,
translations[lang].SATURDAY_SHORT
];
},
monthLongNames,
monthShortNames,
dayLongNames,
dayShortNames,
loadJSON = function (path, success, error) {
var xhr;
if (root.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (success) {
if (root.JSON === undefined) {
alert('Please, include json2 into your project if you want use old browsers!\nhttps://github.com/douglascrockford/JSON-js/blob/master/json2.js');
} else {
success(JSON.parse(xhr.responseText));
}
}
} else {
if (error) {
error(xhr);
}
}
}
};
xhr.open("GET", path, false);
xhr.send();
},
registeredFormats = {
'%d': {
format: function (date) {
return formattingWithNulls(date.day() || tempus.MIN_DAY, 2);
},
parse: function (value) {
var v = Number(value);
return {day: (isNaN(v) ? undefined : v) };
},
minLength: 2,
maxLength: 2,
type: "number"
},
'%m': {
format: function (date) {
return formattingWithNulls(date.month() || tempus.MIN_MONTH, 2);
},
parse: function (value) {
var v = Number(value);
return {month: (isNaN(v) ? undefined : v) };
},
minLength: 2,
maxLength: 2,
type: "number"
},
'%Y': {
format: function (date) {
return formattingWithNulls(date.year() || tempus.MIN_YEAR, 4);
},
parse: function (value) {
var v = Number(value);
return {year: (isNaN(v) ? undefined : v) };
},
minLength: 4,
maxLength: 4,
type: "number"
},
'%w': {
format: function (date) {
return date.dayOfWeek() || tempus.MIN_DAY_OF_WEEK;
},
parse: function () {
// impossible
return {};
},
minLength: 1,
maxLength: 1,
type: "number"
},
'%a': {
format: function (date) {
var d = date.dayOfWeek();
return dayShortNames[d !== undefined ? d : tempus.MIN_DAY_OF_WEEK];
},
parse: function () {
// impossible
return {};
},
minLength: 1,
maxLength: 999,
type: "word"
},
'%A': {
format: function (date) {
var d = date.dayOfWeek();
return dayLongNames[d !== undefined ? d : tempus.MIN_DAY_OF_WEEK];
},
parse: function () {
// impossible
return {};
},
minLength: 1,
maxLength: 999,
type: "word"
},
'%b': {
format: function (date) {
var m = tempus.options('monthFromZero') ? date.month() : date.month()-1;
return monthShortNames[m !== undefined ? m : tempus.MIN_MONTH];
},
parse: function (value) {
var month = tempus.monthNames().indexOf(value) + (tempus.options('monthFromZero') ? 0 : 1);
return {month: month !== -1 ? month : undefined};
},
minLength: 1,
maxLength: 999,
type: "word"
},
'%B': {
format: function (date) {
var m = tempus.options('monthFromZero') ? date.month() : date.month()-1;
return monthLongNames[m !== undefined ? m : tempus.MIN_MONTH];
},
parse: function (value) {
var month = tempus.monthNames(true).indexOf(value) + (tempus.options('monthFromZero') ? 0 : 1);
return {month: month !== -1 ? month : undefined};
},
minLength: 1,
maxLength: 999,
type: "word"
},
'%H': {
format: function (date) {
return formattingWithNulls(date.hours() || tempus.MIN_HOURS, 2);
},
parse: function (value) {
var v = Number(value);
return {hours: (isNaN(v) ? undefined : v) };
},
minLength: 2,
maxLength: 2,
type: "number"
},
'%M': {
format: function (date) {
return formattingWithNulls(date.minutes() || tempus.MIN_MINUTES, 2);
},
parse: function (value) {
var v = Number(value);
return {minutes: (isNaN(v) ? undefined : v) };
},
minLength: 2,
maxLength: 2,
type: "number"
},
'%S': {
format: function (date) {
return formattingWithNulls(date.seconds() || tempus.MIN_SECONDS, 2);
},
parse: function (value) {
var v = Number(value);
return {seconds: (isNaN(v) ? undefined : v) };
},
minLength: 2,
maxLength: 2,
type: "number"
},
'%s': {
format: function (date) {
return date.timestamp();
},
parse: function (value) {
return isNaN(Number(value)) ? {} : tempus(Number(value)).get();
},
minLength: 1,
maxLength: 20,
type: "number"
},
'%F': {
format: function (date) {
return formattingWithNulls(date.year() || tempus.MIN_YEAR, 4) + '-' +
formattingWithNulls(date.month() || tempus.MIN_MONTH, 2) + '-' +
formattingWithNulls(date.day() || tempus.MIN_DAY, 2);
},
parse: function (value) {
var year = Number(value.slice(0, 4)),
month = Number(value.slice(5, 7)),
day = Number(value.slice(8, 10));
return {
year: year,
month: month,
day: day
};
},
minLength: 10,
maxLength: 10,
type: "string"
},
'%D': {
format: function (date) {
return formattingWithNulls(date.month() || tempus.MIN_MONTH, 2) +
'/' + formattingWithNulls(date.day() || tempus.MIN_DAY, 2) +
'/' + formattingWithNulls(date.year() || tempus.MIN_YEAR, 4);
},
parse: function (value) {
var month = Number(value.slice(0, 2)),
day = Number(value.slice(3, 5)),
year = Number(value.slice(6, 10));
return {
year: year,
month: month,
day: day
};
},
minLength: 10,
maxLength: 10,
type: "string"
}
},
options = {
useMilliseconds: false,
monthFromZero: false
},
TempusDate;
/**
* A **TempusDate** class. Store information about some date and can be use
* for working with it date.
* @param {undefined|Date|Object|Array|number|string} options Some date.
* @param {undefined|string} format String for getting date from string or undefined else.
* @param {TempusDate} defaults This object was returning, if parsing failed.
* @return {TempusDate}
* @constructor
*/
TempusDate = function (options, format, defaults) {
// always valid date
this._d = new Date(); // date
// if some errors, write here values.
// incorrect
this._i = {
year: false,
month: false,
day: false,
hours: false,
minutes: false,
seconds: false,
milliseconds: false
};
if (options !== undefined) {
this.set(options, format, defaults);
}
return this;
};
/**
* @doc property
* @name TempusDate.global:fn
* @description
* Short **prototype** alias.
*/
TempusDate.fn = TempusDate.prototype;
/**
* @doc method
* @name TempusDate.global:dayCount
* @return {number} Day count in months.
* @description
* Returns day count in current month.
* ```js
* // returns 30
* tempus([2013, 11, 18]).dayCount();
*
* // returns 29
* tempus([2012, 2]).dayCount();
*
* // returns 28
* tempus([2013, 2]).dayCount();
*
* // returns 31
* tempus([2013, 1]).dayCount();
* ```
*/
TempusDate.fn.dayCount = function () {
var m = this.month(),
dc = tempus.MAX_DAY_IN_MONTHS[m - (tempus.options('monthFromZero') ? 0 : 1)];
if (this.leapYear() && (m === (tempus.options('monthFromZero') ? 1 : 2))) {
dc += 1;
}
return dc;
};
/**
* @doc method
* @name TempusDate.global:year
* @param {number} value Set new year. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set year.
*
* ```js
* // returns current year
* tempus().year();
*
* // returns 2000
* tempus().year(2000).year();
*
* // returns 1000
* tempus().year(1000).year();
*
* // returns 3000
* tempus().year(3000).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year(undefined).year();
*
* // returns 1
* tempus().year(1).year();
*
* // returns -15
* tempus().year(-15).year();
*
* // returns 0
* tempus().year('0').year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year({foo:"bar"}).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year([1,2,3]).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year(null).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year(true).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year(false).year();
*
* // returns 1000 (MIN_YEAR)
* tempus().year(NaN).year();
* ```
*/
TempusDate.fn.year = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_YEAR && Number(value) <= tempus.MAX_YEAR) {
this._d.setFullYear(Number(value));
this._i.year = false;
} else {
this._i.year = Number(value);
}
} else {
this._d.setFullYear(tempus.MIN_YEAR);
this._i.year = false;
}
} else {
return this._i.year === false ? this._d.getFullYear() : this._i.year;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:month
* @param {number} value Set new month. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set month.
*
* ```js
* // returns current month
* tempus().month();
*
* // returns 100
* tempus().month(100).month();
*
* // returns 12
* tempus().month(12).month();
*
* // returns 1
* tempus().month(1).month();
*
* // returns -5
* tempus().month(-5).month();
*
* // returns 0
* tempus().month('0').month();
*
* // returns 1 (MIN_MONTH)
* tempus().month(undefined).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month({foo: 'bar'}).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month([1,2,3]).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month(null).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month(true).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month(false).month();
*
* // returns 1 (MIN_MONTH)
* tempus().month(NaN).month();
* ```
*/
TempusDate.fn.month = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_MONTH && Number(value) <= tempus.MAX_MONTH) {
this._d.setMonth(tempus.options('monthFromZero') ? Number(value) : Number(value) - 1);
this._i.month = false;
} else {
this._i.month = Number(value);
}
} else {
this._d.setMonth(tempus.options('monthFromZero') ? tempus.MIN_MONTH : tempus.MIN_MONTH - 1);
this._i.month = false;
}
} else {
if (this._i.month === false) {
return tempus.options('monthFromZero') ? this._d.getMonth() : (this._d.getMonth()+1);
}
return this._i.month;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:day
* @param {number} value Set new day. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set day of month.
*
* ```js
* // returns current day
* tempus().day();
*
* // returns 100
* tempus().day(100).day();
*
* // returns 12
* tempus().day(12).day();
*
* // returns 1
* tempus().day(1).day();
*
* // returns -5
* tempus().day(-5).day();
*
* // returns 0
* tempus().day('0').day();
*
* // returns 1 (MIN_DAY)
* tempus().day(undefined).day();
*
* // returns 1 (MIN_DAY)
* tempus().day({foo: 'bar'}).day();
*
* // returns 1 (MIN_DAY)
* tempus().day([1,2,3]).day();
*
* // returns 1 (MIN_DAY)
* tempus().day(null).day();
*
* // returns 1 (MIN_DAY)
* tempus().day(true).day();
*
* // returns 1 (MIN_DAY)
* tempus().day(false).day();
*
* // returns 1 (MIN_DAY)
* tempus().day(NaN).day();
* ```
*/
TempusDate.fn.day = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_DAY && Number(value) <= this.dayCount()) {
this._d.setDate(Number(value));
this._i.day = false;
} else {
this._i.day = Number(value);
}
} else {
this._d.setDate(tempus.MIN_DAY);
this._i.day = false;
}
} else {
return this._i.day === false ? this._d.getDate() : this._i.day;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:hours
* @param {number} value Set new hours. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set hours.
*
* ```js
* // returns current hours
* tempus().hours();
*
* // returns 100
* tempus().hours(100).hours();
*
* // returns 12
* tempus().hours(12).hours();
*
* // returns 1
* tempus().hours(1).hours();
*
* // returns -5
* tempus().hours(-5).hours();
*
* // returns 0
* tempus().hours('0').hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours(undefined).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours({foo: 'bar'}).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours([1,2,3]).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours(null).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours(true).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours(false).hours();
*
* // returns 0 (MIN_HOURS)
* tempus().hours(NaN).hours();
* ```
*/
TempusDate.fn.hours = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_HOURS && Number(value) <= tempus.MAX_HOURS) {
this._d.setHours(Number(value));
this._i.hours = false;
} else {
this._i.hours = Number(value);
}
} else {
this._d.setHours(tempus.MIN_HOURS);
this._i.hours = false;
}
} else {
return this._i.hours === false ? this._d.getHours() : this._i.hours;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:minutes
* @param {number} value Set new minutes. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set minutes.
*
* ```js
* // returns current minutes
* tempus().minutes();
*
* // returns 100
* tempus().minutes(100).minutes();
*
* // returns 12
* tempus().minutes(12).minutes();
*
* // returns 1
* tempus().minutes(1).minutes();
*
* // returns -5
* tempus().minutes(-5).minutes();
*
* // returns 0
* tempus().minutes('0').minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes(undefined).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes({foo: 'bar'}).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes([1,2,3]).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes(null).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes(true).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes(false).minutes();
*
* // returns 0 (MIN_MINUTES)
* tempus().minutes(NaN).minutes();
* ```
*/
TempusDate.fn.minutes = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_MINUTES && Number(value) <= tempus.MAX_MINUTES) {
this._d.setMinutes(Number(value));
this._i.minutes = false;
} else {
this._i.minutes = Number(value);
}
} else {
this._d.setMinutes(tempus.MIN_MINUTES);
this._i.minutes = false;
}
} else {
return this._i.minutes === false ? this._d.getMinutes() : this._i.minutes;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:seconds
* @param {number} value Set new seconds. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set seconds.
*
* ```js
* // returns current seconds
* tempus().seconds();
*
* // returns 100
* tempus().seconds(100).seconds();
*
* // returns 12
* tempus().seconds(12).seconds();
*
* // returns 1
* tempus().seconds(1).seconds();
*
* // returns -5
* tempus().seconds(-5).seconds();
*
* // returns 0
* tempus().seconds('0').seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds(undefined).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds({foo: 'bar'}).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds([1,2,3]).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds(null).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds(true).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds(false).seconds();
*
* // returns 0 (MIN_SECONDS)
* tempus().seconds(NaN).seconds();
* ```
*/
TempusDate.fn.seconds = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_SECONDS && Number(value) <= tempus.MAX_SECONDS) {
this._d.setSeconds(Number(value));
this._i.seconds = false;
} else {
this._i.seconds = Number(value);
}
} else {
this._d.setSeconds(tempus.MIN_SECONDS);
this._i.seconds = false;
}
} else {
return this._i.seconds === false ? this._d.getSeconds() : this._i.seconds;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:milliseconds
* @param {number} value Set new milliseconds. If no arguments, returns numeric value.
* @return {TempusDate|number} Returns: if setter - TempusDate, else numeric value.
* @description
* Get or set milliseconds.
*
* ```js
* // returns current milliseconds
* tempus().milliseconds();
*
* // returns 1000
* tempus().milliseconds(1000).milliseconds();
*
* // returns 120
* tempus().milliseconds(12).milliseconds();
*
* // returns 1
* tempus().milliseconds(1).milliseconds();
*
* // returns -5
* tempus().milliseconds(-5).milliseconds();
*
* // returns 0
* tempus().milliseconds('0').milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds(undefined).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds({foo: 'bar'}).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds([1,2,3]).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds(null).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds(true).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds(false).milliseconds();
*
* // returns 0 (MIN_MILLISECONDS)
* tempus().milliseconds(NaN).milliseconds();
* ```
*/
TempusDate.fn.milliseconds = function (value) {
if (arguments.length !== 0) {
if ((typeof value === 'number' || typeof value === 'string') && !isNaN(Number(value))) {
if (Number(value) >= tempus.MIN_MILLISECONDS && Number(value) <= tempus.MAX_MILLISECONDS) {
this._d.setMilliseconds(Number(value));
this._i.milliseconds = false;
} else {
this._i.milliseconds = Number(value);
}
} else {
this._d.setMilliseconds(tempus.MIN_MILLISECONDS);
this._i.milliseconds = false;
}
} else {
return this._i.milliseconds === false ? this._d.getMilliseconds() : this._i.milliseconds;
}
return this;
};
/**
* @doc method
* @name TempusDate.global:week
* @return {number} Week number.
* @description
* Returns week number.
*
* ```js
* // returns 18
* tempus([2013, 5, 1]).week();
* ```
*/
TempusDate.fn.week = function () {
var onejan = new Date(this.year(), 0, 1),
beginDayDate = tempus(this.utc()).hours(0).minutes(0).seconds(0).milliseconds(0).utc()*1000;
return Math.ceil((((beginDayDate - onejan.getTime()) / 86400000) + onejan.getDay() + 1) / 7);
};
/**
* @doc method
* @name TempusDate.global:set
* @param {undefined|Date|Object|Array|number|string} newDate Some date.
* @param {undefined|string} format String for getting date from string or undefined else.
* @param {TempusDate} defaults This object was returning, if parsing failed.
* @return {TempusDate} Date as TempusDate.
* @description
* Set new date. If **undefined**, set now date. If instance of **Date** - set it date.
* If **object**, set date from {year: number, month: number, day: number, hours: number, minutes: number,
* milliseconds: number}. If **Array**, set date from [YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS, MILLISECONDS].
* If **number**, set local time from timestamp. If **string**, set date from formatted date by format (or auto detect
* format). Directives ALWAYS must be started from % and content only 1 char. For example %q, %d, %y, %0.
*
* ```js
* // returns TempusDate with current date
* tempus().set();
*
* // returns TempusDate with date "2013-11-18 20:14:23.918"
* tempus().set({year: 2013, month: 11, day: 18, hours: 20, minutes: 14, seconds: 23, milliseconds: 918});
*
* // returns TempusDate with date "2013-11-18 20:15:38"
* tempus().set(1384791338);
*
* // returns TempusDate with date "2013-01-01 12:00:03"
* tempus().set([2013, 1, 1, 12, 0, 3]);
*
* // returns TempusDate with date "2013-01-01"
* tempus().set(new Date(2012, 0, 1));
*
* // returns TempusDate with date "2013-11-18"
* tempus().set('18.11.2013');
*
* // returns TempusDate with date "2013-12-12"
* tempus().set('2013-12-12', '%Y-%m-%d'));
*
* // returns TempusDate with date "2013-01-01"
* tempus().set('123', '%d.%m.%Y', tempus([2013, 1, 1]));
* ```
*/
TempusDate.fn.set = function (newDate, format, defaults) {
this._i = {
year: false,
month: false,
day: false,
hours: false,
minutes: false,
seconds: false,
milliseconds: false
};
if (newDate === undefined) {
this._d = new Date();
return this;
}
if (newDate instanceof Date) {
this._d = newDate;
return this;
}
if (typeof newDate === 'number') {
this._d = new Date(newDate * (tempus.options('useMilliseconds') ? 1 : 1000));
return this;
}
if (typeof newDate === 'object') {
if (newDate instanceof Array) {
this.day(tempus.MIN_DAY); // if curr day = 29 and curr month not february - month can be set as march, not feb.
this.year(newDate[0]);
this.month(newDate[1]);
this.day(newDate[2]);
this.hours(newDate[3]);
this.minutes(newDate[4]);
this.seconds(newDate[5]);
this.milliseconds(newDate[6]);
} else {
this.day(tempus.MIN_DAY);
this.year(newDate.year);
this.month(newDate.month);
this.day(newDate.day);
this.hours(newDate.hours);
this.minutes(newDate.minutes);
this.seconds(newDate.seconds);
this.milliseconds(newDate.milliseconds);
}
}
// parse date
if (typeof newDate === 'string') {
var key,
lits = [],
parseResult,
directive,
res = [],
i = 0,
j = 0,
k,
shortString,
resultdate = {},
tmpdate;
if (newDate === undefined) {
parseResult = parseBadFormat(this, defaults);
if (parseResult === undefined) {
this._i = {
year: -1,
month: -1,
day: -1,
hours: -1,
minutes: -1,
seconds: -1,
milliseconds: -1
};
}
return parseResult;
}
if (format === undefined) {
format = tempus.detectFormat(newDate);
}
while (i < format.length) {
if (format.charAt(i) === '%') {
directive = format.charAt(i) + format.charAt(i + 1);
if (registeredFormats[directive] !== undefined) {
k = 0;
shortString = '';
switch (registeredFormats[directive].type) {
case 'number':
while ((k < registeredFormats[directive].maxLength) && (j + k < newDate.length) && !isNaN(Number(newDate.charAt(j + k)))) {
shortString += newDate.charAt(j + k);
k += 1;
}
break;
case 'word':
while ((k < registeredFormats[directive].maxLength) && (j + k < newDate.length) && /^[\u00BF-\u1FFF\u2C00-\uD7FF\w]+$/.test(newDate.charAt(j + k))) {
shortString += newDate.charAt(j + k);
k += 1;
}
break;
case 'string':
while ((k < registeredFormats[directive].maxLength) && (j + k < newDate.length)) {
shortString += newDate.charAt(j + k);
k += 1;
}
break;
}
if (k < registeredFormats[directive].minLength) {
parseResult = parseBadFormat(this, defaults);
if (parseResult === undefined) {
this._i = {
year: -1,
month: -1,
day: -1,
hours: -1,
minutes: -1,
seconds: -1,
milliseconds: -1
};
}
return parseResult;
}
lits.push(directive);
res.push(shortString);
k -= 1;
j += k;
i += 1;
}
} else {
if (newDate.charAt(j) !== format.charAt(i)) {
parseResult = parseBadFormat(this, defaults);
if (parseResult === undefined) {
this._i = {
year: -1,
month: -1,
day: -1,
hours: -1,
minutes: -1,
seconds: -1,
milliseconds: -1
};
}
return parseResult;
}
}
i += 1;
j += 1;
}
for (key in lits) {
if (lits.hasOwnProperty(key) && (registeredFormats.hasOwnProperty(lits[key]))) {
tmpdate = registeredFormats[lits[key]].parse(res[key]);
resultdate = {
year: tmpdate.year !== undefined ? tmpdate.year : resultdate.year,
month: tmpdate.month !== undefined ? tmpdate.month : resultdate.month,
day: tmpdate.day !== undefined ? tmpdate.day : resultdate.day,
hours: tmpdate.hours !== undefined ? tmpdate.hours : resultdate.hours,
minutes: tmpdate.minutes !== undefined ? tmpdate.minutes : resultdate.minutes,
seconds: tmpdate.seconds !== undefined ? tmpdate.seconds : resultdate.seconds
};
}
}
this.day(tempus.MIN_DAY);
this.year(resultdate.year);
this.month(resultdate.month);
this.day(resultdate.day);
this.hours(resultdate.hours);
this.minutes(resultdate.minutes);
this.seconds(resultdate.seconds);
this.milliseconds(resultdate.milliseconds);
}
return this;
};
/**
* @doc method
* @name TempusDate.global:leapYear
* @return {boolean} If true year is leap else not leap.
* @description
* Is year leap?
*
* ```js
* // returns false
* tempus([2014]).leapYear();
*
* // returns true
* tempus({year: 2012}).leapYear();
*
* // returns true
* tempus(947698701).leapYear(); // 2012 year
*
* // returns false
* tempus([1900]).leapYear();
*
* // returns false
* tempus({year: 1941, day: 22, month: 6}).leapYear();
*
* // returns true
* tempus({year: 2008, day: 1, month: 1}).leapYear();
*
* // check current year
* tempus().leapYear();
* ```
*/
TempusDate.fn.leapYear = function () {
var year = this.year();
if (year % 4 === 0) {
if (year % 100 === 0) {
return year % 400 === 0;
}
return true;
}
return false;
};
/**
* @doc method
* @name TempusDate.global:timestamp
* @param {number} value Value for set or no value for get.
* @return {TempusDate|number} TempusDate or numeric timestamp.
* @description
* Get or set timestamp.
*
* ```js
* // returns 1384718400
* tempus([2013, 11, 18]).timestamp();
*
* // returns TempusDate with date '2013-11-18'
* tempus().timestamp(1384718400);
* ```
*/
TempusDate.fn.timestamp = function (value) {
if (arguments.length !== 0) {
this._d = new Date(Number(value) * (tempus.options('useMilliseconds') ? 1 : 1000) + this._d.getTimezoneOffset() * 60000);
return this;
}
if (tempus.options('useMilliseconds')) {
return this._d.getTime() - this._d.getTimezoneOffset() * 60000;
}
return Math.floor(this._d.getTime() / 1000) - this._d.getTimezoneOffset() * 60;
};
/**
* @doc method
* @name TempusDate.global:utc
* @param {number} value Value for set or no value for get.
* @return {TempusDate|number} TempusDate or numeric timestamp.
* @description
* Get or set timestamp in UTC.
*
* ```js
* // returns 1384732800
* tempus([2013, 11, 18]).utc();
*
* // returns TempusDate with date '2013-11-18'
* tempus().utc(1384732800);
* ```
*/
TempusDate.fn.