UNPKG

get-moment-stamp

Version:

The getMomentStamp() function returns number of days since 01/01/1970 independent from the current time zone

50 lines (48 loc) 2.1 kB
/*export const toLondonDate_obsolete = (date = new Date()): Date => { return new Date(date.getTime() + date.getTimezoneOffset() * 60000); };*/ const toLondonDate = (date = new Date()) => { const londonTimeZone = 'Europe/London'; return new Date(date.toLocaleString('en-US', { timeZone: londonTimeZone })); }; const GENESIS_STAMP = toLondonDate(new Date(0)); const DIMENSION_DELTA = 1000 * 60 * 60 * 24; const getMomentStamp = (date = new Date()) => { const currentStamp = toLondonDate(date); const differenceMs = Math.abs(currentStamp.getTime() - GENESIS_STAMP.getTime()); return Math.floor(differenceMs / DIMENSION_DELTA); }; const getTimeStamp = (date = new Date()) => { const hour = date.getHours(); const minute = date.getMinutes(); 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) => { const millisecondsSinceGenesis = momentStamp * DIMENSION_DELTA; const londonDate = new Date(GENESIS_STAMP.getTime() + millisecondsSinceGenesis); return new Date(londonDate.getTime() - londonDate.getTimezoneOffset() * 60000); }; const fromTimeStamp = (timeStamp, baseDate = new Date()) => { const hours = Math.floor(timeStamp / 60); const minutes = timeStamp % 60; const resultDate = new Date(baseDate); resultDate.setHours(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.setHours(hours, minutes, 0, 0); return baseDate; }; export { fromMomentStamp, fromTimeStamp, fromTimeStampWithMoment, getMomentStamp, getTimeStamp, isCurrentDate, isCurrentTime, toLondonDate };