UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

67 lines (65 loc) 2.15 kB
import differenceInCalendarDays from 'date-fns/differenceInCalendarDays'; import isBefore from 'date-fns/isBefore'; var FORMATS = /*#__PURE__*/function (FORMATS) { FORMATS["ISO_FORMAT"] = "YYYY-MM-DD"; FORMATS["LOCALIZED_FORMAT"] = "LOCALIZED_FORMAT"; return FORMATS; }(FORMATS || {}); export const timestampToUTCDate = timestamp => { const date = new Date(Number(timestamp)); const day = date.getUTCDate(); const month = date.getUTCMonth() + 1; const year = date.getUTCFullYear(); return { day, month, year }; }; export const todayTimestampInUTC = () => { const today = new Date(Date.now()); const todayInUTC = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()); return todayInUTC.toString(); }; const addLeadingZero = val => { if (val < 10) { return `0${val}`; } return val; }; const capitalizeFirstLetter = str => { return str.charAt(0).toUpperCase() + str.slice(1); }; // example: "23 Jan 2018" export const timestampToString = (timestamp, intl, pattern) => { if (!intl || pattern === FORMATS.ISO_FORMAT) { return timestampToIsoFormat(timestamp); } const date = new Date(Number(timestamp)); return intl.formatDate(date, { timeZone: 'UTC', month: 'short', day: 'numeric', year: 'numeric', formatMatcher: 'best fit' }); }; // example: "2018-01-23" export const timestampToIsoFormat = timestamp => { const date = new Date(Number(timestamp)); return `${date.getUTCFullYear()}-${addLeadingZero(date.getUTCMonth() + 1)}-${date.getUTCDate()}`; }; export const isPastDate = timestamp => { return isBefore(new Date(Number(timestamp)), new Date(Number(todayTimestampInUTC()))); }; export const timestampToTaskContext = (timestamp, intl) => { const curDate = new Date(Number(todayTimestampInUTC())); const givenDate = new Date(Number(timestamp)); const distance = differenceInCalendarDays(givenDate, curDate); if (intl && [-1, 0, 1].indexOf(distance) > -1) { return capitalizeFirstLetter(intl.formatRelativeTime(distance, 'day', { numeric: 'auto' })); } return timestampToString(timestamp, intl, FORMATS.LOCALIZED_FORMAT); };