retail-calendar
Version:
A configurable retail calendar
102 lines (101 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function createDateFromYYYYMMDD(dateString) {
var _a = dateString.split('-').map(parseIntBase10), year = _a[0], month = _a[1], day = _a[2];
var date = newSafeDate();
date.setFullYear(year, month - 1, day);
return date;
}
exports.createDateFromYYYYMMDD = createDateFromYYYYMMDD;
function newSafeDate() {
return new Date(2000, 0, 1);
}
exports.newSafeDate = newSafeDate;
function addDaysToDate(date, days) {
var newDate = newSafeDate();
newDate.setFullYear(date.getFullYear());
newDate.setMonth(date.getMonth());
newDate.setDate(date.getDate() + days);
newDate.setHours(date.getHours());
newDate.setMinutes(date.getMinutes());
newDate.setSeconds(date.getSeconds());
newDate.setMilliseconds(date.getMilliseconds());
return newDate;
}
exports.addDaysToDate = addDaysToDate;
function addWeeksToDate(date, weeks) {
return addDaysToDate(date, weeks * 7);
}
exports.addWeeksToDate = addWeeksToDate;
function startOfDay(date) {
var newDate = new Date(date);
newDate.setHours(0, 0, 0, 0);
return newDate;
}
exports.startOfDay = startOfDay;
function endOfDay(date) {
var newDate = new Date(date);
newDate.setHours(23, 59, 59, 999);
return newDate;
}
exports.endOfDay = endOfDay;
function endOfMonth(date) {
var newDate = new Date(date);
newDate.setMonth(newDate.getMonth() + 1, 0);
newDate.setHours(23, 59, 59, 999);
return newDate;
}
exports.endOfMonth = endOfMonth;
function endOfYear(date) {
var newDate = new Date(date);
newDate.setMonth(11, 31);
newDate.setHours(23, 59, 59, 999);
return newDate;
}
exports.endOfYear = endOfYear;
function getDayDifference(date1, date2) {
var diff = date1.getTime() - date2.getTime();
return Math.floor(diff / (1000 * 60 * 60 * 24));
}
exports.getDayDifference = getDayDifference;
function getWeekDifference(date1, date2) {
var diff = date1.getTime() - date2.getTime();
return Math.floor(diff / (1000 * 60 * 60 * 24 * 7));
}
exports.getWeekDifference = getWeekDifference;
/**
*
* @returns 1 for Monday, 2 for Tuesday, etc. 7 for Sunday
*/
function getIsoWeekDay(date) {
// 0 is Sunday, 1 is Monday, etc.
return date.getDay() === 0 ? 7 : date.getDay();
}
exports.getIsoWeekDay = getIsoWeekDay;
function setIsoWeekDay(date, day) {
var distance = day - getIsoWeekDay(date);
return addDaysToDate(date, distance);
}
exports.setIsoWeekDay = setIsoWeekDay;
function getDayOfYear(date) {
var millisecondsInDay = 1000 * 60 * 60 * 24;
return ((Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
Date.UTC(date.getFullYear(), 0, 0)) /
millisecondsInDay);
}
exports.getDayOfYear = getDayOfYear;
/**
* Returns formatted string in (YYYY-MM-DD) format
*/
function toFormattedString(date) {
// ISO string is in UTC, so we need to convert it to local time by subtracting the timezone offset.
var millisecondsInMinute = 1000 * 60;
return new Date(date.getTime() - date.getTimezoneOffset() * millisecondsInMinute)
.toISOString()
.split('T')[0];
}
exports.toFormattedString = toFormattedString;
function parseIntBase10(value) {
return parseInt(value, 10);
}
exports.parseIntBase10 = parseIntBase10;