UNPKG

@atlaskit/editor-common

Version:

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

67 lines (65 loc) 2.36 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 var timestampToUTCDate = function timestampToUTCDate(timestamp) { var date = new Date(Number(timestamp)); var day = date.getUTCDate(); var month = date.getUTCMonth() + 1; var year = date.getUTCFullYear(); return { day: day, month: month, year: year }; }; export var todayTimestampInUTC = function todayTimestampInUTC() { var today = new Date(Date.now()); var todayInUTC = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()); return todayInUTC.toString(); }; var addLeadingZero = function addLeadingZero(val) { if (val < 10) { return "0".concat(val); } return val; }; var capitalizeFirstLetter = function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); }; // example: "23 Jan 2018" export var timestampToString = function timestampToString(timestamp, intl, pattern) { if (!intl || pattern === FORMATS.ISO_FORMAT) { return timestampToIsoFormat(timestamp); } var 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 var timestampToIsoFormat = function timestampToIsoFormat(timestamp) { var date = new Date(Number(timestamp)); return "".concat(date.getUTCFullYear(), "-").concat(addLeadingZero(date.getUTCMonth() + 1), "-").concat(date.getUTCDate()); }; export var isPastDate = function isPastDate(timestamp) { return isBefore(new Date(Number(timestamp)), new Date(Number(todayTimestampInUTC()))); }; export var timestampToTaskContext = function timestampToTaskContext(timestamp, intl) { var curDate = new Date(Number(todayTimestampInUTC())); var givenDate = new Date(Number(timestamp)); var 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); };