@bemoje/date 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/date v0.0.
|
|
2
|
+
* @bemoje/date v0.0.9
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/"use strict";function isValidDate(year,month,day,hour,minute,second,millisecond){year=year?+year:0,month=month?+month:0,day=day?+day:0,hour=hour?+hour:0,minute=minute?+minute:0,second=second?+second:0,millisecond=millisecond?+millisecond:0;const d=new Date(year,month-1,day,hour,minute,second,millisecond);return d.getFullYear()===year&&d.getMonth()+1===month&&d.getDate()===day&&d.getHours()===hour&&d.getMinutes()===minute&&d.getSeconds()===second&&d.getMilliseconds()===millisecond}function assertValidDate(year,month,day,hour,minute,second,millisecond){if(!isValidDate(year,month,day,hour,minute,second,millisecond))throw new Error(`Expected valid date. Got: ${JSON.stringify({year,month,day,hour,minute,second,millisecond})}.`)}function isValidDateMonth(month){return Number.isInteger(month)&&1<=month&&12>=month}function assertValidDateMonth(month){if(!isValidDateMonth(month))throw new Error(`Invalid month: ${month}.`)}function isValidDateYear(year){return Number.isInteger(year)&&0<=year}function assertValidDateYear(year){if(!isValidDateYear(year))throw new Error(`Invalid year: ${year}.`)}function isLeapYear(year){return assertValidDateYear(year),0==year%4&&0!=year%100||0==year%400}function numDaysInMonth(month,year){assertValidDateMonth(month);return 2===month&&year!==void 0&&isLeapYear(year)?29:[0,31,28,31,30,31,30,31,31,30,31,30,31][month]}function isValidDateDay(day,month,year){return!!Number.isInteger(day)&&!!Number.isInteger(month)&&(!year||Number.isInteger(year))&&!(1>day)&&!(day>numDaysInMonth(month,year))}function assertValidDateDay(day,month,year){if(!isValidDateDay(day,month,year))throw new Error(`Invalid day of the month: ${day}.`)}const MS_IN_DAY=86400000,MS_IN_HOUR=3600000,MS_IN_MINUTE=60000,MS_IN_MONTH=2628000000,MS_IN_SECOND=1e3,MS_IN_WEEK=604800000,MS_IN_YEAR=31536000000;function dateDaysAgo(days){const date=new Date;return date.setDate(date.getDate()-days),date}function dateWeekDayName(date){return names[date.getDay()]}const names=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];function msSinceDate(date){if("number"==typeof date){if(!Number.isInteger(date))throw new Error("Expected date to be a Date or an integer. Got: "+date);date=new Date(date)}return Date.now()-date.getTime()}function daysSinceDate(date){return msSinceDate(date)/MS_IN_DAY}function getCentury(year){return assertValidDateYear(year),Math.floor(year/100)+1}function getCurrentYear(){return new Date().getUTCFullYear()}function hoursSinceDate(date){return msSinceDate(date)/MS_IN_HOUR}function isoDateTimestamp(date=new Date){return date.toISOString().replace(/\D/g,"")}function isoDateTimestampForFilename(date=new Date){return date.toISOString().replace(/[-:T.]/g,"-").replace("Z","")}function minutesSinceDate(date){return msSinceDate(date)/MS_IN_MINUTE}function monthsSinceDate(date){return msSinceDate(date)/MS_IN_MONTH}function prettyIsoDateString(iso,dateTimeSeparator="_",precision="millisecond"){return iso.substring(0,map[precision]).replace(/T/,dateTimeSeparator).replace(/-|:/g,".")}const map={year:4,month:7,day:10,hour:13,minute:16,second:19,millisecond:23};function secondsSinceDate(date){return msSinceDate(date)/MS_IN_SECOND}function weeksSinceDate(date){return msSinceDate(date)/MS_IN_WEEK}function yearsSinceDate(date){return msSinceDate(date)/MS_IN_YEAR}exports.MS_IN_DAY=86400000,exports.MS_IN_HOUR=MS_IN_HOUR,exports.MS_IN_MINUTE=MS_IN_MINUTE,exports.MS_IN_MONTH=MS_IN_MONTH,exports.MS_IN_SECOND=MS_IN_SECOND,exports.MS_IN_WEEK=MS_IN_WEEK,exports.MS_IN_YEAR=MS_IN_YEAR,exports.assertValidDate=assertValidDate,exports.assertValidDateDay=assertValidDateDay,exports.assertValidDateMonth=assertValidDateMonth,exports.assertValidDateYear=assertValidDateYear,exports.dateDaysAgo=dateDaysAgo,exports.dateWeekDayName=dateWeekDayName,exports.daysSinceDate=daysSinceDate,exports.getCentury=getCentury,exports.getCurrentYear=getCurrentYear,exports.hoursSinceDate=hoursSinceDate,exports.isLeapYear=isLeapYear,exports.isValidDate=isValidDate,exports.isValidDateDay=isValidDateDay,exports.isValidDateMonth=isValidDateMonth,exports.isValidDateYear=isValidDateYear,exports.isoDateTimestamp=isoDateTimestamp,exports.isoDateTimestampForFilename=isoDateTimestampForFilename,exports.minutesSinceDate=minutesSinceDate,exports.monthsSinceDate=monthsSinceDate,exports.msSinceDate=msSinceDate,exports.numDaysInMonth=numDaysInMonth,exports.prettyIsoDateString=prettyIsoDateString,exports.secondsSinceDate=secondsSinceDate,exports.weeksSinceDate=weeksSinceDate,exports.yearsSinceDate=yearsSinceDate;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/date v0.0.
|
|
2
|
+
* @bemoje/date v0.0.9
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/function isValidDate(year,month,day,hour,minute,second,millisecond){year=year?+year:0,month=month?+month:0,day=day?+day:0,hour=hour?+hour:0,minute=minute?+minute:0,second=second?+second:0,millisecond=millisecond?+millisecond:0;const d=new Date(year,month-1,day,hour,minute,second,millisecond);return d.getFullYear()===year&&d.getMonth()+1===month&&d.getDate()===day&&d.getHours()===hour&&d.getMinutes()===minute&&d.getSeconds()===second&&d.getMilliseconds()===millisecond}function assertValidDate(year,month,day,hour,minute,second,millisecond){if(!isValidDate(year,month,day,hour,minute,second,millisecond))throw new Error(`Expected valid date. Got: ${JSON.stringify({year,month,day,hour,minute,second,millisecond})}.`)}function isValidDateMonth(month){return Number.isInteger(month)&&1<=month&&12>=month}function assertValidDateMonth(month){if(!isValidDateMonth(month))throw new Error(`Invalid month: ${month}.`)}function isValidDateYear(year){return Number.isInteger(year)&&0<=year}function assertValidDateYear(year){if(!isValidDateYear(year))throw new Error(`Invalid year: ${year}.`)}function isLeapYear(year){return assertValidDateYear(year),0==year%4&&0!=year%100||0==year%400}function numDaysInMonth(month,year){assertValidDateMonth(month);return 2===month&&year!==void 0&&isLeapYear(year)?29:[0,31,28,31,30,31,30,31,31,30,31,30,31][month]}function isValidDateDay(day,month,year){return!!Number.isInteger(day)&&!!Number.isInteger(month)&&(!year||Number.isInteger(year))&&!(1>day)&&!(day>numDaysInMonth(month,year))}function assertValidDateDay(day,month,year){if(!isValidDateDay(day,month,year))throw new Error(`Invalid day of the month: ${day}.`)}const MS_IN_DAY=86400000,MS_IN_HOUR=3600000,MS_IN_MINUTE=60000,MS_IN_MONTH=2628000000,MS_IN_SECOND=1e3,MS_IN_WEEK=604800000,MS_IN_YEAR=31536000000;function dateDaysAgo(days){const date=new Date;return date.setDate(date.getDate()-days),date}function dateWeekDayName(date){return names[date.getDay()]}const names=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];function msSinceDate(date){if("number"==typeof date){if(!Number.isInteger(date))throw new Error("Expected date to be a Date or an integer. Got: "+date);date=new Date(date)}return Date.now()-date.getTime()}function daysSinceDate(date){return msSinceDate(date)/MS_IN_DAY}function getCentury(year){return assertValidDateYear(year),Math.floor(year/100)+1}function getCurrentYear(){return new Date().getUTCFullYear()}function hoursSinceDate(date){return msSinceDate(date)/MS_IN_HOUR}function isoDateTimestamp(date=new Date){return date.toISOString().replace(/\D/g,"")}function isoDateTimestampForFilename(date=new Date){return date.toISOString().replace(/[-:T.]/g,"-").replace("Z","")}function minutesSinceDate(date){return msSinceDate(date)/MS_IN_MINUTE}function monthsSinceDate(date){return msSinceDate(date)/MS_IN_MONTH}function prettyIsoDateString(iso,dateTimeSeparator="_",precision="millisecond"){return iso.substring(0,map[precision]).replace(/T/,dateTimeSeparator).replace(/-|:/g,".")}const map={year:4,month:7,day:10,hour:13,minute:16,second:19,millisecond:23};function secondsSinceDate(date){return msSinceDate(date)/MS_IN_SECOND}function weeksSinceDate(date){return msSinceDate(date)/MS_IN_WEEK}function yearsSinceDate(date){return msSinceDate(date)/MS_IN_YEAR}export{MS_IN_DAY,MS_IN_HOUR,MS_IN_MINUTE,MS_IN_MONTH,MS_IN_SECOND,MS_IN_WEEK,MS_IN_YEAR,assertValidDate,assertValidDateDay,assertValidDateMonth,assertValidDateYear,dateDaysAgo,dateWeekDayName,daysSinceDate,getCentury,getCurrentYear,hoursSinceDate,isLeapYear,isValidDate,isValidDateDay,isValidDateMonth,isValidDateYear,isoDateTimestamp,isoDateTimestampForFilename,minutesSinceDate,monthsSinceDate,msSinceDate,numDaysInMonth,prettyIsoDateString,secondsSinceDate,weeksSinceDate,yearsSinceDate};
|