Socket
Socket
Sign inDemoInstall

simpletime

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpletime - npm Package Compare versions

Comparing version 0.1.29 to 0.2.0

.eslintrc.json

19

package.json
{
"name": "simpletime",
"version": "0.1.29",
"main": "simpletime",
"version": "0.2.0",
"main": "simpletime.dist",
"browser": "simpletime.dist",
"module": "simpletime",
"license": "MIT",

@@ -22,7 +24,16 @@ "readmeFilename": "README.md",

"devDependencies": {
"ava": "^3.11.1"
"@babel/cli": "^7.10.1",
"@babel/core": "^7.10.1",
"@babel/preset-env": "^7.10.1",
"timezone-mock": "^1.1.1",
"ava": "^3.11.1",
"babel-eslint": "^10.0.1",
"eslint": "^7.6.0"
},
"scripts": {
"test": "ava simpletime.spec.js"
"test": "ava simpletime.spec.js",
"lint": "eslint simpletime.js",
"start": "./node_modules/.bin/babel simpletime.js --out-file simpletime.dist.js --presets=@babel/env",
"prepare": "npm start && npm run lint && npm test"
}
}
simpletime
==========
**(c)[Bumblehead][0], 2013,2014,2015,2016** [MIT-license](#license)
**(c)[Bumblehead][0]**
[![npm version](https://badge.fury.io/js/simpletime.svg)](https://badge.fury.io/js/simpletime) [![Build Status](https://travis-ci.org/iambumblehead/simpletime.svg?branch=master)](https://travis-ci.org/iambumblehead/simpletime)
simpletime manages dates and renders them to unicode formats. (i18n through [worldtime][1]).

@@ -6,0 +8,0 @@

// Filename: simpletime.js
// Timestamp: 2017.08.15-00:48:04 (last modified)
// Author(s): Bumblehead (www.bumblehead.com)
//
// parts of this based on work found in comp.lang.javascript faq
//
// simpleTime namespace is defined here with a property named 'localeMethods'.
// localeMethods are redefined by worldTime, which uses this script to provide
// internationalized time formatted methods.
//
// international and local date formats are specified using proper unicode

@@ -15,913 +8,906 @@ // forms http://cldr.unicode.org/translation/date-time

var simpletime = module.exports = (function () {
function isNum (n) {
return !isNaN(parseFloat(n));
}
const isNum = n => !Number.isNaN(Number.parseFloat(n));
return {
// used by worldTime script
// localeMap : {},
localeMethods_getDateSymbolsMonthAbbrev : () => {
return {
1:"Jan",
2:"Feb",
3:"Mar",
4:"Apr",
5:"May",
6:"Jun",
7:"Jul",
8:"Aug",
9:"Sep",
10:"Oct",
11:"Nov",
12:"Dec"
};
},
localeMethods_getDateSymbolsMonthWide : () => {
return {
1:"January",
2:"February",
3:"March",
4:"April",
5:"May",
6:"June",
7:"July",
8:"August",
9:"September",
10:"October",
11:"November",
12:"December"
};
},
localeMethods_getDateSymbolsMonthNarrow : () => {
return {
1:"J",
2:"F",
3:"M",
4:"A",
5:"M",
6:"J",
7:"J",
8:"A",
9:"S",
10:"O",
11:"N",
12:"D"
};
},
localeMethods_getDateSymbolsDayAbbrev : () => {
return [
"sun",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat"
];
},
localeMethods_getDateSymbolsDayWide : () => {
return [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
},
const dateRegex = {
iso : /^\s*(\d{4})[-/\.](\d\d)[-/\.](\d\d)\s*$/,
usEndian : /^\s*(\d\d)[-/\.](\d\d)[-/\.](\d{4})\s*$/
};
// abbreviated month name, ex. `Jan` or `Dec`
localeMethods_getNumericMonthNameAbbrev : function (monthNum) {
return this.localeMethods_getDateSymbolsMonthAbbrev()[monthNum];
},
const defFormatRegex = {
iso : /yyyy[/.-]mm[/.-]dd/gi,
usEndian : /mm[/.-]dd[/.-]yyyy/gi
};
// wide month name, ex. `January` or `December`
localeMethods_getNumericMonthNameWide : function (monthNum) {
return this.localeMethods_getDateSymbolsMonthWide()[monthNum];
},
const localeMethods = {
getDateSymbolsMonthAbbrev : () => ({
1 : "Jan",
2 : "Feb",
3 : "Mar",
4 : "Apr",
5 : "May",
6 : "Jun",
7 : "Jul",
8 : "Aug",
9 : "Sep",
10 : "Oct",
11 : "Nov",
12 : "Dec"
}),
// abbreviated day name, ex. `sun` or `thu`
localeMethods_getNumericDayNameAbbrev : function (dayNum) {
return this.localeMethods_getDateSymbolsDayAbbrev()[dayNum];
},
getDateSymbolsMonthWide : () => ({
1 : "January",
2 : "February",
3 : "March",
4 : "April",
5 : "May",
6 : "June",
7 : "July",
8 : "August",
9 : "September",
10 : "October",
11 : "November",
12 : "December"
}),
// wide day name, ex. `Sunday` or `Thursday`
localeMethods_getNumericDayNameWide : function (dayNum) {
return this.localeMethods_getDateSymbolsDayWide()[dayNum];
},
getDateSymbolsMonthNarrow : () => ({
1 : "J",
2 : "F",
3 : "M",
4 : "A",
5 : "M",
6 : "J",
7 : "J",
8 : "A",
9 : "S",
10 : "O",
11 : "N",
12 : "D"
}),
isDateObj: function (dateObj) {
return (typeof dateObj === 'object' &&
dateObj && 'getTime' in dateObj) ? true : false;
},
dateRegex: {
iso: /^\s*(\d{4})[-/\.](\d\d)[-/\.](\d\d)\s*$/,
usEndian: /^\s*(\d\d)[-/\.](\d\d)[-/\.](\d{4})\s*$/
},
defFormatRegex: {
iso: /yyyy[/.-]mm[/.-]dd/gi,
usEndian: /mm[/.-]dd[/.-]yyyy/gi
},
getDateSymbolsDayAbbrev : () => ([
"sun",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat"
]),
// WARNING: THESE METHODS ASSUME VALID INPUT
getDateSymbolsDayWide : () => ([
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
])
};
// ex. 2013, 0008, 0488
getDateYStr: (d) =>
("000" + d.getFullYear()).slice(-4),
// abbreviated month name, ex. `Jan` or `Dec`
const localeMethods_getNumericMonthNameAbbrev = monthNum => (
localeMethods.getDateSymbolsMonthAbbrev()[monthNum]);
// ex. 12, 10, 07, 04
getDateMStr: (d) =>
("0" + (d.getMonth() + 1)).slice(-2),
// wide month name, ex. `January` or `December`
const localeMethods_getNumericMonthNameWide = monthNum => (
localeMethods.getDateSymbolsMonthWide()[monthNum]);
// ex. 12, 10, 07, 04, 30
getDateDStr: (d) =>
("0" + d.getDate()).slice(-2),
// abbreviated day name, ex. `sun` or `thu`
const localeMethods_getNumericDayNameAbbrev = dayNum => (
localeMethods.getDateSymbolsDayAbbrev()[dayNum]);
getDatehhStr: (d) => ("0" + d.getHours()).slice(-2),
getDatemmStr: (d) => ("0" + d.getMinutes()).slice(-2),
getDatessStr: (d) => ("0" + d.getSeconds()).slice(-2),
// wide day name, ex. `Sunday` or `Thursday`
const localeMethods_getNumericDayNameWide = dayNum => (
localeMethods.getDateSymbolsDayWide()[dayNum]);
// ex. 2013, 8, 488
getDateYNum: (d) =>
d.getFullYear(),
const isDateObj = dateObj => dateObj instanceof Date && !Number.isNaN(dateObj);
// ex. 12, 10, 7, 4
getDateMNum: (d) =>
d.getMonth() + 1,
// ex. 2013, 0008, 0488
const getDateYStr = d => ("000" + d.getFullYear()).slice(-4);
// ex. 12, 10, 7, 4, 30
getDateDNum: (d) =>
d.getDate(),
// ex. 12, 10, 07, 04
const getDateMStr = d => ("0" + (d.getMonth() + 1)).slice(-2);
getDateYMDStrArr: function (d) {
var that = this;
if (that.isDateObj(d)) {
return [
that.getDateYStr(d),
that.getDateMStr(d),
that.getDateDStr(d)
//that.getDatehhStr(d),
//that.getDatemmStr(d),
//that.getDatessStr(d)
];
};
},
// ex. 12, 10, 07, 04, 30
const getDateDStr = d => ("0" + d.getDate()).slice(-2);
// return the date as array of numbers
// ex. [2013, 5, 5]
getDateYMDNumArr: function (d) {
var that = this;
if (that.isDateObj(d)) {
return [
that.getDateYNum(d),
that.getDateMNum(d),
that.getDateDNum(d)
];
};
},
const getDatehhStr = d => ("0" + d.getHours()).slice(-2);
const getDatemmStr = d => ("0" + d.getMinutes()).slice(-2);
const getDatessStr = d => ("0" + d.getSeconds()).slice(-2);
// return date object from string OR number formatted ymdArr
getYMDArrDate: (YMDArr = []) => {
let [y = 0, m = 0, d = 0, hh = 0, mm = 0, ss = 0, ms = 0] = YMDArr,
date = null;
// ex. 2013, 8, 488
const getDateYNum = d => d.getFullYear();
if (isNum(y) && isNum(m) && isNum(d)) {
date = new Date();
date.setFullYear(+y, +m - 1, +d);
date.setHours(
isNum(hh) ? +hh : 0,
isNum(mm) ? +mm : 0,
isNum(ss) ? +ss : 0,
isNum(ms) ? +ms : 0
);
}
// ex. 12, 10, 7, 4
const getDateMNum = d => d.getMonth() + 1;
return date;
},
// ex. 12, 10, 7, 4, 30
const getDateDNum = d => d.getDate();
const getDateYMDStrArr = d => isDateObj(d) ? [
getDateYStr(d),
getDateMStr(d),
getDateDStr(d)
//that.getDatehhStr(d),
//that.getDatemmStr(d),
//that.getDatessStr(d)
] : [];
// return the date as array of numbers
// ex. [2013, 5, 5]
const getDateYMDNumArr = d => isDateObj(d) ? [
getDateYNum(d),
getDateMNum(d),
getDateDNum(d)
]: [];
// return a date, with given number of hours added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
getSecFromDate: (dateObj, optNum) =>
(new Date(dateObj.getTime() + optNum * 1000)),
// return a new date object that has time of the given dateObj, defined to
// the first second of the day.
const getTimeBgnDay = dateObj => {
var d = new Date(dateObj || null);
getSecFromTodayDate: function (optNum) {
return this.getMinFromDate(new Date(), optNum);
},
d.setMilliseconds(0);
d.setSeconds(0);
d.setMinutes(0);
d.setHours(0);
return d;
};
// return a new date object that has time of the given dateObj, defined to
// the last second of the day.
const getTimeEndDay = dateObj => {
var d = new Date(dateObj || null);
d.setMilliseconds(998);
d.setSeconds(59);
d.setMinutes(59);
d.setHours(23);
return d;
};
// return a date, with given number of minutes added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
getMinFromDate: (dateObj, optNum) =>
(new Date(dateObj.getTime() + optNum * 60 * 1000)),
// return number days that occur in the given month for the given year
const getDaysInMonth = (yNum, mNum) => (
new Date(yNum, mNum, 0).getDate());
getMinFromTodayDate: function (optNum) {
return this.getMinFromDate(new Date(), optNum);
},
// if M is greater than 12, a value of 12 (representing M) is returned
// if M is less than 12, a value of 0 (representing M) is returned
const getMFittedYMDNumArr = YMDNumArr => {
let [ y, m, d ] = YMDNumArr;
if (m <= 0) {
m = 1;
} else if (m > 12) {
m = 12;
}
return [ y, m, d ];
};
// return a date, with given number of hours added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
getHourFromDate: (dateObj, optNum) =>
(new Date(dateObj.getTime() + optNum * 60 * 60 * 1000)),
// this method will return a YMDNumArr with a value of 'D' fitted to fall
// in the range of the date created by Y and M.
//
// for example, if M has 28 days and D is 30, 28 is returned,
const getDFittedYMDNumArr = YMDNumArr => {
let [ y, m, d ] = YMDNumArr,
ymDays = getDaysInMonth(y, m);
getHourFromTodayDate: function (optNum) {
return this.getMinFromDate(new Date(), optNum);
},
if (d <= 0) {
d = 1;
} else if (d > ymDays) {
d = ymDays;
}
return [ y, m, d ];
};
// return a date, with given number of days added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
getDayFromDate: function (dateObj, optNum) {
var that = this, num = 0, finDate = null,
YMDNumArr = that.getDateYMDNumArr(dateObj || new Date());
// return date object from string OR number formatted ymdArr
const getYMDArrDate = (YMDArr = []) => {
let [ y = 0, m = 0, d = 0, hh = 0, mm = 0, ss = 0, ms = 0 ] = YMDArr,
date = null;
if (YMDNumArr) {
if (typeof optNum === 'string') {
if (isNum(optNum)) {
num = parseInt(+optNum, 10);
}
} else if (typeof optNum === 'number') {
num = +optNum;
}
if (isNum(y) && isNum(m) && isNum(d)) {
date = new Date();
date.setFullYear(+y, +m - 1, +d);
date.setHours(
isNum(hh) ? +hh : 0,
isNum(mm) ? +mm : 0,
isNum(ss) ? +ss : 0,
isNum(ms) ? +ms : 0
);
}
if (num) YMDNumArr[2] += num;
finDate = that.getYMDArrDate(YMDNumArr);
}
return date;
};
return finDate;
},
// return a date, with given number of minutes added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
const getMinFromDate = (dateObj, optNum) => (
new Date(dateObj.getTime() + optNum * 60 * 1000));
getDayFromTodayDate: function (optNum) {
return this.getDayFromDate(new Date(), optNum);
},
const getMinFromTodayDate = optNum => (
getMinFromDate(new Date(), optNum));
// return a date, with given number of months added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
//
// if original date is on day 30 and new month is only 28 days long, the
// returned month will have its day 'fitted' so that the new date object
// will be defined to the latest day of the month
getMonthFromDate: function (dateObj, optNum) {
var that = this, num = 0, finDate = null, diff,
YMDNumArr = that.getDateYMDNumArr(dateObj || new Date());
// return a date, with given number of hours added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
const getSecFromDate = (dateObj, optNum) => (
new Date(dateObj.getTime() + optNum * 1000));
if (YMDNumArr) {
if (typeof optNum === 'string') {
if (isNum(optNum)) {
num = parseInt(+optNum, 10);
}
} else if (typeof optNum === 'number') {
num = +optNum;
}
const getSecFromTodayDate = optNum => (
getMinFromDate(new Date(), optNum));
if (num) {
YMDNumArr[1] += num;
YMDNumArr = that.getDFittedYMDNumArr(YMDNumArr);
}
finDate = that.getYMDArrDate(YMDNumArr);
}
return finDate;
},
// return a date, with given number of hours added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
const getHourFromDate = (dateObj, optNum) => (
new Date(dateObj.getTime() + optNum * 60 * 60 * 1000));
getMonthFromTodayDate: function (opt) {
return this.getDayFromDate(new Date(), opt);
},
const getHourFromTodayDate = optNum => (
getMinFromDate(new Date(), optNum));
// return a date, with given number of years added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
getYearFromDate: function (dateObj, optNum) {
var that = this,
YMDArr = that.getDateYMDNumArr(dateObj || new Date()),
newDate, diff;
// return a date, with given number of days added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
const getDayFromDate = (dateObj, optNum) => {
var num = 0, finDate = null,
YMDNumArr = getDateYMDNumArr(dateObj || new Date());
if (YMDNumArr) {
if (typeof optNum === 'string') {
if (isNum(optNum)) {
YMDArr[0] += +optNum;
num = Number.parseInt(+optNum, 10);
}
} else if (typeof optNum === 'number') {
num = +optNum;
}
return that.getYMDArrDate(YMDArr);
},
if (num) YMDNumArr[2] += num;
finDate = getYMDArrDate(YMDNumArr);
}
getYearFromTodayDate: function (optNum) {
return this.getYearFromDate(new Date(), optNum);
},
return finDate;
};
// http://stackoverflow.com/questions/1184334/get-number-days-in-a-specified-month-using-javascript
// return number of days that occur in the given month for the given year
getDaysInMonth : function (yNum, mNum) {
return new Date(yNum, mNum, 0).getDate();
},
const getDayFromTodayDate = optNum => (
getDayFromDate(new Date(), optNum));
// if M is greater than 12, a value of 12 (representing M) is returned
// if M is less than 12, a value of 0 (representing M) is returned
getMFittedYMDNumArr : function (YMDNumArr) {
var that = this, daysInMonthNum,
y = YMDNumArr[0],
m = YMDNumArr[1],
d = YMDNumArr[2];
// return a date, with given number of months added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
//
// if original date is on day 30 and new month is only 28 days long, the
// returned month will have its day 'fitted' so that the new date object
// will be defined to the latest day of the month
const getMonthFromDate = (dateObj, optNum) => {
var num = 0, finDate = null, diff,
YMDNumArr = getDateYMDNumArr(dateObj || new Date());
if (m <= 0) {
m = 1;
} else if (m > 12) {
m = 12;
if (YMDNumArr) {
if (typeof optNum === 'string') {
if (isNum(optNum)) {
num = Number.parseInt(+optNum, 10);
}
} else if (typeof optNum === 'number') {
num = +optNum;
}
return [y, m, d];
},
if (num) {
YMDNumArr[1] += num;
YMDNumArr = getDFittedYMDNumArr(YMDNumArr);
}
finDate = getYMDArrDate(YMDNumArr);
}
// this method will return a YMDNumArr with a value of 'D' fitted to fall
// in the range of the date created by Y and M.
//
// for example, if M has 28 days and D is 30, 28 is returned,
getDFittedYMDNumArr : function (YMDNumArr) {
var that = this, daysInMonthNum,
y = YMDNumArr[0],
m = YMDNumArr[1],
d = YMDNumArr[2],
ymDays = that.getDaysInMonth(y, m);
return finDate;
};
if (d <= 0) {
d = 1;
} else if (d > ymDays) {
d = ymDays;
}
const getMonthFromTodayDate = opt => (
getDayFromDate(new Date(), opt));
return [y, m, d];
},
// return a date, with given number of years added
// number may be negative so that the returned date will be in the past
//
// optNum: +2 goes forward two. -6 go back 6.
const getYearFromDate = (dateObj, optNum) => {
var YMDArr = getDateYMDNumArr(dateObj || new Date());
// return a new date object defined from the given date object
// returned date is defined to the first day of the month
//
// if optNum is provided, optNum days are added to date object
getFirstOfMonth: function (dateObj, optNum) {
var that = this,
YMDNumArr = that.getDateYMDNumArr(new Date(dateObj || null)),
finDate;
if (isNum(optNum)) {
YMDArr[0] += +optNum;
}
YMDNumArr[2] = 1;
if (isNum(optNum)) {
YMDNumArr[2] += +optNum;
}
finDate = that.getYMDArrDate(YMDNumArr);
finDate = that.getTimeBgnDay(finDate);
return getYMDArrDate(YMDArr);
};
return finDate;
},
const getYearFromTodayDate = optNum => (
getYearFromDate(new Date(), optNum));
// return a new date object defined from the given date object
// returned date is defined to the last day of the month
getLastOfMonth: function (dateObj) {
return new Date(dateObj.getFullYear(), dateObj.getMonth() + 1, 0);
},
// return a new date object defined from the given date object
// returned date is defined to the first day of the month
//
// if optNum is provided, optNum days are added to date object
const getFirstOfMonth = (dateObj, optNum) => {
var YMDNumArr = getDateYMDNumArr(new Date(dateObj || null)),
finDate;
// return true if the first date occurs `before` the second date
isDateBeforeDate: function (dateObj, beforeDateObj) {
var now = beforeDateObj,
dateObj1 = dateObj,
year, yearNow, month, monthNow, date, dateNow;
YMDNumArr[2] = 1;
if (isNum(optNum)) {
YMDNumArr[2] += +optNum;
}
finDate = getYMDArrDate(YMDNumArr);
finDate = getTimeBgnDay(finDate);
year = dateObj1.getFullYear();
yearNow = now.getFullYear();
if (year <= yearNow) {
month = dateObj1.getMonth();
monthNow = now.getMonth();
if (year <= yearNow && month <= monthNow) {
date = dateObj1.getDate();
dateNow = now.getDate();
if (year <= yearNow && month <= monthNow && date <= dateNow) {
return true;
}
}
}
return false;
},
return finDate;
};
// return true if the date occurs `before` today's date
isDateBeforeToday: function (dateObj) {
return this.isDateBeforeDate(dateObj, new Date());
},
// return a new date object defined from the given date object
// returned date is defined to the last day of the month
const getLastOfMonth = dateObj => (
new Date(dateObj.getFullYear(), dateObj.getMonth() + 1, 0));
// return a new date object that has time of the given dateObj, defined to
// the first second of the first day of the month.
getTimeBgnMonth: function (dateObj) {
var that = this, dateYMDArr = that.getDateYMDStrArr(dateObj), finDateObj;
dateYMDArr[2] = 1;
finDateObj = that.getYMDArrDate(dateYMDArr);
finDateObj = that.getTimeBgnDay(finDateObj);
return finDateObj;
},
// return true if the first date occurs `before` the second date
const isDateBeforeDate = (dateObj, beforeDateObj) => {
var now = beforeDateObj,
dateObj1 = dateObj,
year, yearNow, month, monthNow, date, dateNow;
// return a new date object that has time of the given dateObj, defined to
// the last second of the last day of the month.
getTimeEndMonth: function (dateObj) {
var that = this, dateYMDArr = that.getMonthFromDate(dateObj, 1), finDateObj;
year = dateObj1.getFullYear();
yearNow = now.getFullYear();
if (year <= yearNow) {
month = dateObj1.getMonth();
monthNow = now.getMonth();
if (year <= yearNow && month <= monthNow) {
date = dateObj1.getDate();
dateNow = now.getDate();
if (year <= yearNow && month <= monthNow && date <= dateNow) {
return true;
}
}
}
return false;
};
dateYMDArr = that.getDateYMDStrArr(dateYMDArr);
dateYMDArr[2] = 0; // end of month
finDateObj = that.getYMDArrDate(dateYMDArr);
finDateObj = that.getTimeEndDay(finDateObj);
return finDateObj;
},
// return true if the date occurs `before` today's date
const isDateBeforeToday = dateObj => (
isDateBeforeDate(dateObj, new Date()));
// return a new date object that has time of the given dateObj, defined to
// the first second of the day.
getTimeBgnDay: function (dateObj) {
var d = new Date(dateObj || null);
// return a new date object that has time of the given dateObj, defined to
// the first second of the first day of the month.
const getTimeBgnMonth = dateObj => {
var dateYMDArr = getDateYMDStrArr(dateObj), finDateObj;
d.setMilliseconds(0);
d.setSeconds(0);
d.setMinutes(0);
d.setHours(0);
return d;
},
dateYMDArr[2] = 1;
finDateObj = getYMDArrDate(dateYMDArr);
finDateObj = getTimeBgnDay(finDateObj);
return finDateObj;
};
// return a new date object that has time of the given dateObj, defined to
// the last second of the day.
getTimeEndDay: function (dateObj) {
var d = new Date(dateObj || null);
// return a new date object that has time of the given dateObj, defined to
// the last second of the last day of the month.
const getTimeEndMonth = dateObj => {
var dateYMDArr = getMonthFromDate(dateObj, 1), finDateObj;
d.setMilliseconds(998);
d.setSeconds(59);
d.setMinutes(59);
d.setHours(23);
return d;
},
dateYMDArr = getDateYMDStrArr(dateYMDArr);
dateYMDArr[2] = 0; // end of month
finDateObj = getYMDArrDate(dateYMDArr);
finDateObj = getTimeEndDay(finDateObj);
return finDateObj;
};
// ISO 8601
// accept time as: yyyy.mm.dd, yyyy/mm/dd, yyyy-mm-dd
//
// return a date object from a simple ISO formatted date
parseISO8601: function (dateStringInRange) {
var date = new Date(NaN), month,
parts = dateStringInRange.match(this.dateRegex.iso);
// ISO 8601
// accept time as: yyyy.mm.dd, yyyy/mm/dd, yyyy-mm-dd
//
// return a date object from a simple ISO formatted date
const parseISO8601 = dateStringInRange => {
var date = new Date(NaN), month,
parts = dateStringInRange.match(dateRegex.iso);
if (!parts) return null;
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if (month != date.getMonth() + 1) (date = date.getTime(NaN));
return (date && isNum(date.getTime())) ? date : null;
},
if (!parts) return null;
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if (month != date.getMonth() + 1) (date = date.getTime(NaN));
return (date && isNum(date.getTime())) ? date : null;
};
isDefISOFormat: function (format) {
return format.match(this.defFormatRegex.iso) ? true : false;
},
const isDefISOFormat = format => defFormatRegex.iso.test(format);
// middle endian
// accept time as: mm.dd.yyyy, mm/dd/yyyy, mm-dd-yyyy
//
// return a date object from a simple US endian formatted date
parseUSEndian: function (dateStringInRange) {
var date = new Date(NaN), month,
parts = dateStringInRange.match(this.dateRegex.usEndian);
// middle endian
// accept time as: mm.dd.yyyy, mm/dd/yyyy, mm-dd-yyyy
//
// return a date object from a simple US endian formatted date
const parseUSEndian = dateStringInRange => {
var date = new Date(NaN), month,
parts = dateStringInRange.match(dateRegex.usEndian);
if (!parts) return null;
month = +parts[1];
date.setFullYear(parts[3], month - 1, parts[2]);
if (month != date.getMonth() + 1) (date = date.getTime(NaN));
return (date && isNum(date.getTime())) ? date : null;
},
if (!parts) return null;
month = +parts[1];
date.setFullYear(parts[3], month - 1, parts[2]);
if (month != date.getMonth() + 1) (date = date.getTime(NaN));
return (date && isNum(date.getTime())) ? date : null;
};
isDefUSEndianFormat: function (format) {
return format.match(this.defFormatRegex.usEndian) ? true : false;
},
const isDefUSEndianFormat = format => defFormatRegex.usEndian.test(format);
// return a simple ISO formatted date from a date object
// returns a date object in iso standard: yyyy/mm/dd
getDateAsISO: function (dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
// return a simple ISO formatted date from a date object
// returns a date object in iso standard: yyyy/mm/dd
const getDateAsISO = dateInRange => {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
if (!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return yyyy + "/" + mm + "/" + dd;
},
if (!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return yyyy + "/" + mm + "/" + dd;
};
getEpochDateAsISO: function (strEpoch) {
var num, date = null;
const getEpochDateAsISO = strEpoch => {
var num, date = null;
if (strEpoch && isNum(strEpoch)) {
num = parseInt(strEpoch, 10);
date = new Date(num);
if (date) date = this.getDateAsISO(date);
}
return date;
},
if (strEpoch && isNum(strEpoch)) {
num = Number.parseInt(strEpoch, 10);
date = new Date(num);
if (date) date = getDateAsISO(date);
}
return date;
};
// return a simple US endian formatted date from a date object
// returns a date object in iso standard: mm-dd-yyyy
getDateAsUSEndian: function (dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
// return a simple US endian formatted date from a date object
// returns a date object in iso standard: mm-dd-yyyy
const getDateAsUSEndian = dateInRange => {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
if (!isInRange) throw RangeError("formatDate: year must be 0000-9999");
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return mm + "/" + dd + '/' + yyyy;
},
if (!isInRange) throw RangeError("formatDate: year must be 0000-9999");
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return mm + "/" + dd + '/' + yyyy;
};
// accepts a string or int epoch
getEpochDateAsUSEndian: function (strEpoch) {
var num, date;
if (strEpoch && (isNum(strEpoch))) {
num = parseInt(strEpoch, 10);
date = new Date(num);
if (date) return this.getDateAsUSEndian(date);
}
return null;
},
// accepts a string or int epoch
const getEpochDateAsUSEndian = strEpoch => {
var num, date;
if (strEpoch && isNum(strEpoch)) {
num = Number.parseInt(strEpoch, 10);
date = new Date(num);
if (date) return getDateAsUSEndian(date);
}
return null;
};
// return monthly array of dates within the range of bgnDate and endDate. An
// optional filter function fn may be provided as the third parameter.
//
// bgnDate will be the first element in the returned array
yieldRangeMonthly: function (bgnDate, endDate, filter) {
var that = this, date = new Date(bgnDate), dateArr = [],
endD = that.getTimeEndMonth(endDate);
// return monthly array of dates within the range of bgnDate and endDate. An
// optional filter function fn may be provided as the third parameter.
//
// bgnDate will be the first element in the returned array
const yieldRangeMonthly = (bgnDate, endDate, filter) => {
var date = new Date(bgnDate), dateArr = [],
endD = getTimeEndMonth(endDate);
filter = filter || function (d) {
return d;
};
filter = filter || (d => d);
while (date < endD) {
dateArr.push(filter(date));
date = that.getMonthFromDate(date, 1);
}
while (date < endD) {
dateArr.push(filter(date));
date = getMonthFromDate(date, 1);
}
return dateArr;
},
return dateArr;
};
// return daily array of dates within the range of bgnDate and endDate. An
// optional filter function fn may be provided as the third parameter.
//
// bgnDate will be the first element in the returned array
yieldRangeDaily: function (bgnDate, endDate, filter) {
var bgnD = new Date(bgnDate),
endD = new Date(endDate),
that = this,
dateArr = [],
f = filter || function (o) {
return o;
};
// return daily array of dates within the range of bgnDate and endDate. An
// optional filter function fn may be provided as the third parameter.
//
// bgnDate will be the first element in the returned array
const yieldRangeDaily = (bgnDate, endDate, filter) => {
var bgnD = new Date(bgnDate),
endD = new Date(endDate),
dateArr = [],
f = filter || (d => d);
while (bgnD < endD) {
dateArr.push(f(new Date(bgnD)));
bgnD = that.getDayFromDate(bgnD, 1);
}
while (bgnD < endD) {
dateArr.push(f(new Date(bgnD)));
bgnD = getDayFromDate(bgnD, 1);
}
return dateArr;
},
return dateArr;
};
// return an object whose properties define the elapsed time between
// bgnDate and endDate
getElapsedTimeObj: function (bgnDate, endDate) {
var ms = endDate.getTime() - bgnDate.getTime(),
floor = Math.floor;
// return an object whose properties define the elapsed time between
// bgnDate and endDate
const getElapsedTimeObj = (bgnDate, endDate) => {
var ms = endDate.getTime() - bgnDate.getTime(),
{ floor } = Math;
return {
ms : floor(ms) % 1000,
sec : floor(ms / 1000) % 60, // ms in sec : 1000
min : floor(ms / 60000) % 60, // ms in min : 1000 * 60,
hour : floor(ms / 3600000) % 24, // ms in hour : 1000 * 60 * 60,
day : floor(ms / 86400000) // ms in day : 1000 * 60 * 60 * 24;
};
},
return {
ms : floor(ms) % 1000,
sec : floor(ms / 1000) % 60, // ms in sec : 1000
min : floor(ms / 60000) % 60, // ms in min : 1000 * 60,
hour : floor(ms / 3600000) % 24, // ms in hour : 1000 * 60 * 60,
day : floor(ms / 86400000) // ms in day : 1000 * 60 * 60 * 24;
};
};
getElapsedTimeFormatted: function (bgnDate, endDate) {
var e = this.getElapsedTimeObj(bgnDate, endDate),
min = ((e.min.length > 1) ? '' : '0') + e.min,
sec = ((e.sec.length > 1) ? '' : '0') + e.sec,
ms = (e.ms.length > 2) ? (e.ms[0] + e.ms[1]) : e.ms;
const getElapsedTimeFormatted = (bgnDate, endDate) => {
var e = getElapsedTimeObj(bgnDate, endDate),
min = ((e.min.length > 1) ? '' : '0') + e.min,
sec = ((e.sec.length > 1) ? '' : '0') + e.sec,
ms = (e.ms.length > 2) ? (e.ms[0] + e.ms[1]) : e.ms;
return min + ':' + (sec.length ? '' : '0') +
sec + ':' + ms + ' (mm:ss:ms)';
},
return min + ':' + (sec.length ? '' : '0') +
sec + ':' + ms + ' (mm:ss:ms)';
};
// return true if given dates fall within the same range specified by `type`
//
// are dates within the same `day`?
// are dates within the same `month`?
// are dates within the same `year`?
//
// type === 'month' | 'year' | 'day'
isDatesInRange: function (dateObj1, dateObj2, type) {
var that = this,
d1YMDArr = that.getDateYMDStrArr(dateObj1),
d2YMDArr = that.getDateYMDStrArr(dateObj2),
isInRange = false;
// return true if given dates fall within the same range specified by `type`
//
// are dates within the same `day`?
// are dates within the same `month`?
// are dates within the same `year`?
//
// type === 'month' | 'year' | 'day'
const isDatesInRange = (dateObj1, dateObj2, type) => {
var d1YMDArr = getDateYMDStrArr(dateObj1),
d2YMDArr = getDateYMDStrArr(dateObj2),
isInRange = false;
if (type === 'day') {
isInRange = (d1YMDArr[0] === d2YMDArr[0] &&
d1YMDArr[1] === d2YMDArr[1] &&
d1YMDArr[2] === d2YMDArr[2]);
} else if (type === 'month') {
isInRange = (d1YMDArr[0] === d2YMDArr[0] &&
d1YMDArr[1] === d2YMDArr[1]);
} else if (type === 'year') {
isInRange = d1YMDArr[0] === d2YMDArr[0];
}
return isInRange;
},
if (type === 'day') {
isInRange = (d1YMDArr[0] === d2YMDArr[0] &&
d1YMDArr[1] === d2YMDArr[1] &&
d1YMDArr[2] === d2YMDArr[2]);
} else if (type === 'month') {
isInRange = (d1YMDArr[0] === d2YMDArr[0] &&
d1YMDArr[1] === d2YMDArr[1]);
} else if (type === 'year') {
isInRange = d1YMDArr[0] === d2YMDArr[0];
}
return isInRange;
};
// return a date that is formatted according to the given unicode formatStr
// http://cldr.unicode.org/translation/date-time
//
// using this date object:
// var date = new Date(1365222221485),
//
// each of the following formats would return a result given below:
// format: "MMMM d, y h:mm:ss a z"
// return: "April 5, 2013 9:23:41 pm 420"
//
// format: "MMM d, y h:mm:ss a"
// return: "Apr 5, 2013 9:23:41 pm"
//
// format: "M/d/yyyy h:mm a"
// return: "4/5/2013 9:23 pm"
applyFormatDate: function (date, format, d = new Date(date)) {
var that = this, YMDArr, year = d.getFullYear(),
isInRange = year >= 0 && year <= 9999, hour,
localeMethods = that.localeMethods,
tzRe = /'[^']*'|"[^"]*"|yyyyy|yyyy|yyy|yy|y|MMMMM|MMMM|MMM|MM|M|ddddd|dddd|ddd|dd|d|EEEEE|EEEE|EEE|EE|E|hh|h|HH|H|mm|m|ss|s|zzzz|z|a|v/g;
// return a date that is formatted according to the given unicode formatStr
// http://cldr.unicode.org/translation/date-time
//
// using this date object:
// var date = new Date(1365222221485),
//
// each of the following formats would return a result given below:
// format: "MMMM d, y h:mm:ss a z"
// return: "April 5, 2013 9:23:41 pm 420"
//
// format: "MMM d, y h:mm:ss a"
// return: "Apr 5, 2013 9:23:41 pm"
//
// format: "M/d/yyyy h:mm a"
// return: "4/5/2013 9:23 pm"
const applyFormatDate = (date, format, d = new Date(date)) => {
var YMDArr, year = d.getFullYear(),
isInRange = year >= 0 && year <= 9999, hour,
// eslint-disable-next-line max-len
tzRe = /'[^']*'|"[^"]*"|yyyyy|yyyy|yyy|yy|y|MMMMM|MMMM|MMM|MM|M|ddddd|dddd|ddd|dd|d|EEEEE|EEEE|EEE|EE|E|hh|h|HH|H|mm|m|ss|s|zzzz|z|a|v/g;
if (!isInRange) {
//throw RangeError("formatDate: year must be 0000-9999");
}
YMDArr = that.getDateYMDStrArr(d);
if (!isInRange) {
//throw RangeError("formatDate: year must be 0000-9999");
}
YMDArr = getDateYMDStrArr(d);
return format.replace(tzRe, function (match) {
switch (match) {
case "y":
// year, numeric, full digit year, 1 to 4 digits
return YMDArr[0].replace(/^00?0?/, '');
case "yy":
// year, numeric, exactly two digit
return YMDArr[0].slice(2, 4);
case "yyyy":
// year, numeric, full year
return YMDArr[0];
case "yyyyy":
// year, narrow, one char
return YMDArr[0].slice(3, 4);
case "M":
// month, numeric, at least one digit
return YMDArr[1].replace(/^0/, '');
case "MM":
// month, numeric, at least two digits, 0-padding
return YMDArr[1];
case "MMM":
// month, alpha, abbreviated string
return that.localeMethods_getNumericMonthNameAbbrev(YMDArr[1].replace(/^0/, ''));
case "MMMM":
// month, alpha, full string
return that.localeMethods_getNumericMonthNameWide(YMDArr[1].replace(/^0/, ''));
case "MMMMM":
// month, alpha, narrow, one char
return that.localeMethods_getNumericMonthNameAbbrev(YMDArr[1].replace(/^0/, ''))[0];
case "d":
// day, numeric, at least one digit
return YMDArr[2].replace(/^0/, '');
case "dd":
// day, numeric, at least two digits, 0-padding
return YMDArr[2];
case "ddd":
// day, alpha, abbreviated string
return that.localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "dddd":
// day, alpha, full string
return that.localeMethods_getNumericDayNameWide(YMDArr[2].replace(/^0/, ''));
case "ddddd":
// day, alpha, narrow, one char
return that.localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''))[0];
case "E":
// day, alpha, abbreviated string
return that.localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EE":
// day, alpha, abbreviated string
return that.localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EEE":
// day, alpha, abbreviated string
return that.localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EEEE":
// day, alpha, full string
return that.localeMethods_getNumericDayNameWide(YMDArr[2].replace(/^0/, ''));
case "EEEEE":
// day, numeric, at least one digit
return YMDArr[2].replace(/^0*/, '');
case "h":
// hour, numeric, at least one digit
return (d.getHours() % 12) || 12;
case "hh":
// hour, numeric, 12pm at least two digits, 0-padding
return ("0" + ((d.getHours() % 12) || 12)).slice(-2);
case "HH":
// hour, numeric, 24pm at least two digits, 0-padding
return ("0" + d.getHours()).slice(-2);
case "H":
// hour, numeric, 24pm
return d.getHours();
case "m":
// minute, numeric, at least one digit
return d.getMinutes();
case "mm":
// minute, numeric, at least two digits, 0-padding
return ("0" + d.getMinutes()).slice(-2);
case "s":
// second, numeric, at least one digit
return d.getSeconds();
case "ss":
// second, numeric, at least two digits, 0-padding
return ("0" + d.getSeconds()).slice(-2);
case "a":
// am, a, pm, p, noon, n (only used with `h`)
return (d.getHours() < 12) ? 'am' : 'pm';
case "zzzz":
//????http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript
// timezone, numeric, at least 4 digits, 0-padding
return ("000" + d.getTimezoneOffset()).slice(-4);
case "z":
// timezone, numeric
return d.getTimezoneOffset();
case "v":
// timezone, numeric
return d.getTimezoneOffset();
default:
return match;
}
return format.replace(tzRe, match => {
switch (match) {
case "y":
// year, numeric, full digit year, 1 to 4 digits
return YMDArr[0].replace(/^00?0?/, '');
case "yy":
// year, numeric, exactly two digit
return YMDArr[0].slice(2, 4);
case "yyyy":
// year, numeric, full year
return YMDArr[0];
case "yyyyy":
// year, narrow, one char
return YMDArr[0].slice(3, 4);
case "M":
// month, numeric, at least one digit
return YMDArr[1].replace(/^0/, '');
case "MM":
// month, numeric, at least two digits, 0-padding
return YMDArr[1];
case "MMM":
// month, alpha, abbreviated string
// eslint-disable-next-line max-len
return localeMethods_getNumericMonthNameAbbrev(YMDArr[1].replace(/^0/, ''));
case "MMMM":
// month, alpha, full string
return localeMethods_getNumericMonthNameWide(YMDArr[1].replace(/^0/, ''));
case "MMMMM":
// month, alpha, narrow, one char
// eslint-disable-next-line max-len
return localeMethods_getNumericMonthNameAbbrev(YMDArr[1].replace(/^0/, ''))[0];
case "d":
// day, numeric, at least one digit
return YMDArr[2].replace(/^0/, '');
case "dd":
// day, numeric, at least two digits, 0-padding
return YMDArr[2];
case "ddd":
// day, alpha, abbreviated string
return localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "dddd":
// day, alpha, full string
return localeMethods_getNumericDayNameWide(YMDArr[2].replace(/^0/, ''));
case "ddddd":
// day, alpha, narrow, one char
// eslint-disable-next-line max-len
return localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''))[0];
case "E":
// day, alpha, abbreviated string
return localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EE":
// day, alpha, abbreviated string
return localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EEE":
// day, alpha, abbreviated string
return localeMethods_getNumericDayNameAbbrev(YMDArr[2].replace(/^0/, ''));
case "EEEE":
// day, alpha, full string
return localeMethods_getNumericDayNameWide(YMDArr[2].replace(/^0/, ''));
case "EEEEE":
// day, numeric, at least one digit
return YMDArr[2].replace(/^0*/, '');
case "h":
// hour, numeric, at least one digit
return (d.getHours() % 12) || 12;
case "hh":
// hour, numeric, 12pm at least two digits, 0-padding
return ("0" + ((d.getHours() % 12) || 12)).slice(-2);
case "HH":
// hour, numeric, 24pm at least two digits, 0-padding
return ("0" + d.getHours()).slice(-2);
case "H":
// hour, numeric, 24pm
return d.getHours();
case "m":
// minute, numeric, at least one digit
return d.getMinutes();
case "mm":
// minute, numeric, at least two digits, 0-padding
return ("0" + d.getMinutes()).slice(-2);
case "s":
// second, numeric, at least one digit
return d.getSeconds();
case "ss":
// second, numeric, at least two digits, 0-padding
return ("0" + d.getSeconds()).slice(-2);
case "a":
// am, a, pm, p, noon, n (only used with `h`)
return (d.getHours() < 12) ? 'am' : 'pm';
case "zzzz":
// timezone, numeric, at least 4 digits, 0-padding
return ("000" + d.getTimezoneOffset()).slice(-4);
case "z":
// timezone, numeric
return d.getTimezoneOffset();
case "v":
// timezone, numeric
return d.getTimezoneOffset();
default:
return match;
}
});
},
});
};
// remove all chars. all formatting
// tokenize around whitespace
// 10/1/2012 => 10 1 2012
// M/d/yyyy => M d yyyy
//
// will break given:
// dStr: 10/1/2012039
// format: M/d/yyyy
extractDateFormatted: function (dStr, format) {
var that = this, x, ymdArr = [0,0,0,0,0,0,0,0], ymdTestArr, token, tokenItem, finDateObj,
formatRaw = format.replace(/[^\d\w]/gi, ' '),
dStrRaw = dStr.replace(/[^\d\w]/gi, ' '),
formatTokens = formatRaw.split(' '),
dStrTokens = dStrRaw.split(' ');
// remove all chars. all formatting
// tokenize around whitespace
// 10/1/2012 => 10 1 2012
// M/d/yyyy => M d yyyy
//
// will break given:
// dStr: 10/1/2012039
// format: M/d/yyyy
const extractDateFormatted = (dStr, format) => {
var x, ymdArr = [ 0,0,0,0,0,0,0,0 ], ymdTestArr, token, tokenItem, finDateObj,
formatRaw = format.replace(/[^\d\w]/gi, ' '),
dStrRaw = dStr.replace(/[^\d\w]/gi, ' '),
formatTokens = formatRaw.split(' '),
dStrTokens = dStrRaw.split(' ');
function getAsISO(tokenItem, dateStrObj) {
var tokenItemL = tokenItem.toLowerCase();
function getAsISO (tokenItem, dateStrObj) {
var tokenItemL = tokenItem.toLowerCase();
//return Object.keys(dateStrObj)
// .find(key => dateStrObj[key].toLowerCase() === tokenItemL) || '';
for (var o in dateStrObj) {
if (dateStrObj.hasOwnProperty(o)) {
if (dateStrObj[o].toLowerCase() === tokenItemL) return o;
}
}
return '';
//return Object.keys(dateStrObj)
// .find(key => dateStrObj[key].toLowerCase() === tokenItemL) || '';
for (var o in dateStrObj) {
if (dateStrObj.hasOwnProperty(o)) {
if (dateStrObj[o].toLowerCase() === tokenItemL) return o;
}
}
return '';
}
formatTokens.map(function (token, x) {
tokenItem = dStrTokens[x];
formatTokens.forEach((token, x) => {
tokenItem = dStrTokens[x];
if (token.match(/yyyyy|yyyy|yyy|yy|y/)) {
if (token === "y" && isNum(tokenItem)) {
// year, numeric, full digit year, 1 to 4 digits
if (tokenItem.match(/^\d\d?\d?\d?$/)) {
ymdArr[0] = tokenItem + '';
}
} else if (token === "yy" && isNum(tokenItem)) {
// year, numeric, exactly two digit
if (tokenItem.match(/^\d\d$/)) {
ymdArr[0] = tokenItem + '';
}
} else if (token === "yyyy" && isNum(tokenItem)) {
// year, numeric, full year
if (tokenItem.match(/^\d\d?\d?\d?$/)) {
ymdArr[0] = tokenItem + '';
}
} else if (token === "yyyyy" && isNum(tokenItem)) {
// year, narrow, one char
if (tokenItem.match(/^\d?$/)) {
ymdArr[0] = tokenItem + '';
}
}
} else if (token.match(/MMMMM|MMMM|MMM|MM|M/)) {
if (token === "M" && isNum(tokenItem)) {
// month, numeric, at least one digit
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[1] = tokenItem + '';
}
} else if (token === "MM" && isNum(tokenItem)) {
// month, numeric, at least two digits, 0-padding
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[1] = tokenItem + '';
}
} else if (token === "MMM") {
// month, alpha, abbreviated string
ymdArr[1] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsMonthAbbrev());
} else if (token === "MMMM") {
// month, alpha, full string
ymdArr[1] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsMonthWide());
} else if (token === "MMMMM") {
// month, alpha, narrow, one char
ymdArr[1] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsMonthNarrow());
}
} else if (token.match(/ddddd|dddd|ddd|dd|d/)) {
if (token === "d" && isNum(tokenItem)) {
// day, numeric, at least one digit
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[2] = tokenItem + '';
}
} else if (token === "dd" && isNum(tokenItem)) {
// day, numeric, at least two digits, 0-padding
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[2] = tokenItem + '';
}
} else if (token === "ddd") {
// day, alpha, abbreviated string
ymdArr[2] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsDayAbbrev());
} else if (token === "dddd") {
// day, alpha, full string
ymdArr[2] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsDayWide());
} else if (token === "ddddd") {
// day, alpha, narrow, one char
ymdArr[2] = getAsISO(tokenItem, that.localeMethods_getDateSymbolsDayNarrow());
}
} else if (token.match(/h|H/) && isNum(tokenItem)) {
//hour. h for 12 hour, H for 24.
if (token.match(/h/)) {
ymdArr[3] = +tokenItem % 12 || 12;
} else {
ymdArr[3] = tokenItem + '';
}
} else if (token.match(/mm?/) && isNum(tokenItem)) {
//minute
ymdArr[4] = tokenItem + '';
} else if (token.match(/ss?/) && isNum(tokenItem)) {
//second
ymdArr[5] = tokenItem + '';
} else if (token.match(/a/)) {
// am, a, pm, p, noon, n
if (/pm?/.test( tokenItem )) {
// assumes pm occurs after hour
ymdArr[3] += 12;
}
} else if (token.match(/v|z/)) {
// Pacific Time, Paris Time
ymdArr[6] = tokenItem + '';
if (token.match(/yyyyy|yyyy|yyy|yy|y/)) {
if (token === "y" && isNum(tokenItem)) {
// year, numeric, full digit year, 1 to 4 digits
if (tokenItem.match(/^\d\d?\d?\d?$/)) {
ymdArr[0] = tokenItem + '';
}
});
if (ymdArr[0] && ymdArr[1] && ymdArr[2]) {
// if `m` or `d` values are too large or small, dateObj is still
// generated as per specificiation `15.9 Date Objects`
// to avoid confusion we invalidate such dates by reconverting them
// to ymdArr and checking the values to be sure they match
//
// ex. '01/40/1958' would not pass through here
if ((finDateObj = that.getYMDArrDate(ymdArr))) {
ymdTestArr = that.getDateYMDNumArr(finDateObj);
if (ymdTestArr[0] === +ymdArr[0] &&
ymdTestArr[1] === +ymdArr[1] &&
ymdTestArr[2] === +ymdArr[2]) {
return finDateObj;
}
} else if (token === "yy" && isNum(tokenItem)) {
// year, numeric, exactly two digit
if (tokenItem.match(/^\d\d$/)) {
ymdArr[0] = tokenItem + '';
}
} else if (token === "yyyy" && isNum(tokenItem)) {
// year, numeric, full year
if (tokenItem.match(/^\d\d?\d?\d?$/)) {
ymdArr[0] = tokenItem + '';
}
} else if (token === "yyyyy" && isNum(tokenItem)) {
// year, narrow, one char
if (tokenItem.match(/^\d?$/)) {
ymdArr[0] = tokenItem + '';
}
}
} else if (token.match(/MMMMM|MMMM|MMM|MM|M/)) {
if (token === "M" && isNum(tokenItem)) {
// month, numeric, at least one digit
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[1] = tokenItem + '';
}
} else if (token === "MM" && isNum(tokenItem)) {
// month, numeric, at least two digits, 0-padding
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[1] = tokenItem + '';
}
} else if (token === "MMM") {
// month, alpha, abbreviated string
// eslint-disable-next-line max-len
ymdArr[1] = getAsISO(tokenItem, localeMethods.getDateSymbolsMonthAbbrev());
} else if (token === "MMMM") {
// month, alpha, full string
// eslint-disable-next-line max-len
ymdArr[1] = getAsISO(tokenItem, localeMethods.getDateSymbolsMonthWide());
} else if (token === "MMMMM") {
// month, alpha, narrow, one char
// eslint-disable-next-line max-len
ymdArr[1] = getAsISO(tokenItem, localeMethods.getDateSymbolsMonthNarrow());
}
} else if (token.match(/ddddd|dddd|ddd|dd|d/)) {
if (token === "d" && isNum(tokenItem)) {
// day, numeric, at least one digit
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[2] = tokenItem + '';
}
} else if (token === "dd" && isNum(tokenItem)) {
// day, numeric, at least two digits, 0-padding
if (tokenItem.match(/^\d\d?$/)) {
ymdArr[2] = tokenItem + '';
}
} else if (token === "ddd") {
// day, alpha, abbreviated string
// eslint-disable-next-line max-len
ymdArr[2] = getAsISO(tokenItem, localeMethods.getDateSymbolsDayAbbrev());
} else if (token === "dddd") {
// day, alpha, full string
ymdArr[2] = getAsISO(tokenItem, localeMethods.getDateSymbolsDayWide());
} else if (token === "ddddd") {
// day, alpha, narrow, one char
// eslint-disable-next-line max-len
ymdArr[2] = getAsISO(tokenItem, localeMethods.getDateSymbolsDayNarrow());
}
} else if (token.match(/h|H/) && isNum(tokenItem)) {
//hour. h for 12 hour, H for 24.
if (token.match(/h/)) {
ymdArr[3] = +tokenItem % 12 || 12;
} else {
ymdArr[3] = tokenItem + '';
}
} else if (token.match(/mm?/) && isNum(tokenItem)) {
//minute
ymdArr[4] = tokenItem + '';
} else if (token.match(/ss?/) && isNum(tokenItem)) {
//second
ymdArr[5] = tokenItem + '';
} else if (token.match(/a/)) {
// am, a, pm, p, noon, n
if (/pm?/.test(tokenItem)) {
// assumes pm occurs after hour
ymdArr[3] += 12;
}
} else if (token.match(/v|z/)) {
// Pacific Time, Paris Time
ymdArr[6] = tokenItem + '';
}
};
});
}());
if (ymdArr[0] && ymdArr[1] && ymdArr[2]) {
// if `m` or `d` values are too large or small, dateObj is still
// generated as per specificiation `15.9 Date Objects`
// to avoid confusion we invalidate such dates by reconverting them
// to ymdArr and checking the values to be sure they match
//
// ex. '01/40/1958' would not pass through here
if ((finDateObj = getYMDArrDate(ymdArr))) {
ymdTestArr = getDateYMDNumArr(finDateObj);
if (ymdTestArr[0] === +ymdArr[0] &&
ymdTestArr[1] === +ymdArr[1] &&
ymdTestArr[2] === +ymdArr[2]) {
return finDateObj;
}
}
}
};
export default {
isNum,
dateRegex,
defFormatRegex,
localeMethods,
localeMethods_getNumericMonthNameAbbrev,
localeMethods_getNumericMonthNameWide,
localeMethods_getNumericDayNameAbbrev,
localeMethods_getNumericDayNameWide,
isDateObj,
getDateYStr,
getDateMStr,
getDateDStr,
getDatehhStr,
getDatemmStr,
getDatessStr,
getDateYNum,
getDateMNum,
getDateDNum,
getDateYMDStrArr,
getDateYMDNumArr,
getYMDArrDate,
getSecFromDate,
getSecFromTodayDate,
getMinFromDate,
getMinFromTodayDate,
getHourFromDate,
getHourFromTodayDate,
getDayFromDate,
getDayFromTodayDate,
getMonthFromDate,
getMonthFromTodayDate,
getYearFromDate,
getYearFromTodayDate,
getDaysInMonth,
getMFittedYMDNumArr,
getDFittedYMDNumArr,
getFirstOfMonth,
getLastOfMonth,
isDateBeforeDate,
isDateBeforeToday,
getTimeBgnMonth,
getTimeEndMonth,
getTimeBgnDay,
getTimeEndDay,
parseISO8601,
isDefISOFormat,
parseUSEndian,
isDefUSEndianFormat,
getDateAsISO,
getEpochDateAsISO,
getDateAsUSEndian,
getEpochDateAsUSEndian,
yieldRangeMonthly,
yieldRangeDaily,
getElapsedTimeObj,
getElapsedTimeFormatted,
isDatesInRange,
applyFormatDate,
extractDateFormatted
};

@@ -6,16 +6,17 @@ // Filename: simpletime.spec.js

const test = require('ava');
const simpletime = require('../simpletime');
const timezone_mock = require('timezone-mock');
const simpletime = require('.').default;
//Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)
timezone_mock.register('US/Pacific');
test("isDateObj, should return false if input is null", t => {
t.is( simpletime.isDateObj(null), false );
t.is(simpletime.isDateObj(null), false);
});
test("isDateObj, should return false if input is undefined", t => {
t.is( simpletime.isDateObj(null), false );
t.is(simpletime.isDateObj(null), false);
});
test("isDateObj, should return false if input is {}", t => {
t.is( simpletime.isDateObj({}), false );
t.is(simpletime.isDateObj({}), false);
});

@@ -27,3 +28,3 @@

t.is( simpletime.isDateObj(date), true );
t.is(simpletime.isDateObj(date), true);
});

@@ -35,3 +36,3 @@

t.is( simpletime.getDateYStr(date), '2013' );
t.is(simpletime.getDateYStr(date), '2013');
});

@@ -42,3 +43,3 @@

t.is( simpletime.getDateMStr(date), '04' );
t.is(simpletime.getDateMStr(date), '04');
});

@@ -50,3 +51,3 @@

t.is( simpletime.getDateDStr(date), '05' );
t.is(simpletime.getDateDStr(date), '05');
});

@@ -59,3 +60,3 @@

t.deepEqual( result, ['2013', '04', '05' ]);
t.deepEqual(result, [ '2013', '04', '05' ]);
});

@@ -68,5 +69,6 @@

t.deepEqual( result, [ 2013, 4, 5 ]);
t.deepEqual(result, [ 2013, 4, 5 ]);
});
// eslint-disable-next-line max-len
test("getDateYMDArr, should return the correct date object from YMDStrArr", t => {

@@ -84,2 +86,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getMinFromDate, should return a new date one minute ahead of the given date object", t => {

@@ -93,2 +96,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getMFittedYMDNumArr, should return a new YMDNumArr with a correct month greater than 12", t => {

@@ -102,2 +106,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getMFittedYMDNumArr, should return a new YMDNumArr with a correct month less than 1", t => {

@@ -111,2 +116,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getMFittedYMDNumArr, should return a new YMDNumArr with same month if 1 <= month <= 12", t => {

@@ -120,2 +126,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getDFittedYMDNumArr, should return a new YMDNumArr with a correct day greater than possible days in month", t => {

@@ -129,2 +136,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getDFittedYMDNumArr, should return a new YMDNumArr with a correct day less than 1", t => {

@@ -138,2 +146,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getDFittedYMDNumArr, should return a new YMDNumArr with same day if 1 <= month <= possible days of month at year", t => {

@@ -177,4 +186,5 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

t.is(result, 30 );
});
});
// eslint-disable-next-line max-len
test("getDayFromDate, should return a new date one day ahead of the given date object", t => {

@@ -188,2 +198,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getMonthFromDate, should return a new date one month ahead of the given date object", t => {

@@ -197,2 +208,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getYearFromDate, should return a new date one year ahead of the given date object", t => {

@@ -206,2 +218,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDateBeforeDate, should return true for a date that is before a date", t => {

@@ -216,2 +229,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDateBeforeDate, should return false for a date that is before a date", t => {

@@ -226,2 +240,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getTimeBgnMonth, should return true for a date defined to the beginning of the month", t => {

@@ -243,2 +258,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getTimeEndMonth, should return true for a date defined to the end of the month", t => {

@@ -260,2 +276,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getTimeBgnDay, should return true for a date defined to the beginning of the day", t => {

@@ -277,2 +294,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getTimeBgnDay, should return a date defined to the beginning of the day", t => {

@@ -290,2 +308,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("parseISO8601, should return a date from a simplified ISO formatted date, `2013/12/20`", t => {

@@ -302,3 +321,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("parseUSEndian, should return a date from a simplified US Endian formatted date, `12/20/2013`", t => {

@@ -315,2 +334,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getDateAsISO, should return a simplified ISO formatted date from a date, `2013/4/5`", t => {

@@ -323,2 +343,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getDateUSEndian, should return a simplified US Endian formatted date from a date, `4/5/2013`", t => {

@@ -332,2 +353,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// first date will be 3/3/2010
// eslint-disable-next-line max-len
test("yieldRangeMonthly, should return 38 dates twixt `3/3/2010` and `4/5/2013`", t => {

@@ -344,2 +366,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// first date will be 3/3/2010
// eslint-disable-next-line max-len
test("yieldRangeDaily, should return 3 dates twixt `4/5/2013, 21:23` and `4/7/2013, 17:40`", t => {

@@ -360,2 +383,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// first date will be 3/3/2010
// eslint-disable-next-line max-len
test("yieldRangeDaily, should return 4 dates twixt `4/5/2013, 21:23` and `4/7/2013, 23:59`", t => {

@@ -377,2 +401,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, full", t => {

@@ -388,2 +413,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, medium", t => {

@@ -399,2 +425,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, short", t => {

@@ -410,2 +437,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `dd-MM-yy`", t => {

@@ -421,2 +449,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `M/d/yy`", t => {

@@ -430,2 +459,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `dd-MM-yyyy`", t => {

@@ -439,2 +469,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `MMM d, y`", t => {

@@ -448,2 +479,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `MMMM d, y`", t => {

@@ -457,2 +489,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `5 'de' April 'de' 2013`", t => {

@@ -466,2 +499,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `EEEE, d 'de' MMMM 'de' y`", t => {

@@ -475,2 +509,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("applyFormatDate, should return a correctly formatted date from date object, `EEEE, MMMM d, y`", t => {

@@ -484,2 +519,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("getElapsedTime, should return an object whose properties describe elapsed time, ", t => {

@@ -499,2 +535,3 @@ //Sun Apr 07 2013 18:08:45 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDatesInRange, should return true when dates are within the same day", t => {

@@ -509,2 +546,3 @@ //Sun Apr 07 2013 18:08:45 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDatesInRange, should return false when dates are not within the same day", t => {

@@ -519,2 +557,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDatesInRange, should return true when dates are within the same month", t => {

@@ -529,2 +568,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDatesInRange, should return false when dates are within the different month", t => {

@@ -539,2 +579,3 @@ //Wed Mar 03 2010 17:32:44 GMT-0800 (PST)

// eslint-disable-next-line max-len
test("isDatesInRange, should return true when dates are within the same year", t => {

@@ -549,2 +590,3 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

// eslint-disable-next-line max-len
test("isDatesInRange, should return false when dates are not within the same year", t => {

@@ -559,2 +601,3 @@ //Wed Mar 03 2010 17:32:44 GMT-0800 (PST)

// eslint-disable-next-line max-len
test("extractDateFormatted, should return a correct date object form formatted date, full", t => {

@@ -570,2 +613,3 @@ const date = new Date(1365308621485),

// eslint-disable-next-line max-len
test("extractDateFormatted, should return a correct date object form formatted date, medium", t => {

@@ -572,0 +616,0 @@ //Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc