get-moment-stamp
Version:
Pure-UTC time math: install it and forget about time zones, DST and host locale. Encodes any instant as two flat integers — whole UTC days since 1970-01-01 and minutes since 00:00 UTC — so cache keys and schedulers stay stable across Docker rebuilds and t
46 lines (43 loc) • 1.6 kB
JavaScript
;
const DIMENSION_DELTA = 1000 * 60 * 60 * 24;
const getMomentStamp = (date = new Date()) => {
return Math.floor(date.getTime() / DIMENSION_DELTA);
};
const getTimeStamp = (date = new Date()) => {
const hour = date.getUTCHours();
const minute = date.getUTCMinutes();
return hour * 60 + minute;
};
const isCurrentTime = (timeStamp, delta = 15) => {
const currentStamp = getTimeStamp();
const min = currentStamp - delta;
const max = currentStamp + delta;
return timeStamp >= min && timeStamp <= max;
};
const isCurrentDate = (date, stamp = getMomentStamp()) => {
return getMomentStamp(date) === stamp;
};
const fromMomentStamp = (momentStamp) => {
return new Date(momentStamp * DIMENSION_DELTA);
};
const fromTimeStamp = (timeStamp, baseDate = new Date()) => {
const hours = Math.floor(timeStamp / 60);
const minutes = timeStamp % 60;
const resultDate = new Date(baseDate);
resultDate.setUTCHours(hours, minutes, 0, 0);
return resultDate;
};
const fromTimeStampWithMoment = (timeStamp, momentStamp = getMomentStamp()) => {
const baseDate = fromMomentStamp(momentStamp);
const hours = Math.floor(timeStamp / 60);
const minutes = timeStamp % 60;
baseDate.setUTCHours(hours, minutes, 0, 0);
return baseDate;
};
exports.fromMomentStamp = fromMomentStamp;
exports.fromTimeStamp = fromTimeStamp;
exports.fromTimeStampWithMoment = fromTimeStampWithMoment;
exports.getMomentStamp = getMomentStamp;
exports.getTimeStamp = getTimeStamp;
exports.isCurrentDate = isCurrentDate;
exports.isCurrentTime = isCurrentTime;