UNPKG

@chayns-components/date

Version:

A set of beautiful React components for developing your own applications with chayns.

139 lines 3.47 kB
import { getLanguage, Language } from 'chayns-api'; import { isCurrentYear, isToday, isTomorrow, isYesterday } from './date'; export const getDateInfo = _ref => { let { date, shouldShowYear, shouldShowTime, shouldShowDayOfWeek, shouldShowRelativeDayOfWeek, shouldUseShortText } = _ref; const { active: language } = getLanguage(); let dayPart = ''; if (shouldShowRelativeDayOfWeek) { const rtf = new Intl.RelativeTimeFormat(language, { numeric: 'auto' }); if (isToday(date)) { dayPart = capitalizeFirstLetter(rtf.format(0, 'day')); } if (isTomorrow(date)) { dayPart = capitalizeFirstLetter(rtf.format(1, 'day')); } if (isYesterday(date)) { dayPart = capitalizeFirstLetter(rtf.format(-1, 'day')); } } if (!dayPart && shouldShowDayOfWeek) { dayPart = date.toLocaleDateString(language, { weekday: shouldUseShortText ? 'short' : 'long' }); } const dateParts = { day: '2-digit', month: shouldUseShortText ? 'short' : 'long' }; if (shouldShowYear && !isCurrentYear(date)) { dateParts.year = 'numeric'; } const timeParts = {}; if (shouldShowTime) { timeParts.hour = '2-digit'; timeParts.minute = '2-digit'; } let formattedTime = ''; if (Object.keys(timeParts).length > 0) { formattedTime = `, ${date.toLocaleTimeString(language, { ...timeParts })}`; } const hourWord = getTimeString({ language }); formattedTime += shouldShowTime ? ` ${hourWord}` : ''; const formattedDate = `${date.toLocaleDateString(language, dateParts)}${formattedTime}`; return `${dayPart}${dayPart ? ', ' : ''}${formattedDate}`; }; const capitalizeFirstLetter = text => text.charAt(0).toUpperCase() + text.slice(1); export const getTimeTillNow = _ref2 => { let { date, currentDate, language = Language.English } = _ref2; const diffInSeconds = Math.floor((currentDate.getTime() - date.getTime()) / 1000); const isPast = diffInSeconds > 0; const units = [{ label: 'year', seconds: 31536000 }, { label: 'month', seconds: 2592000 }, { label: 'day', seconds: 86400 }, { label: 'hour', seconds: 3600 }, { label: 'minute', seconds: 60 }, { label: 'second', seconds: 1 }]; const absDiff = Math.abs(diffInSeconds); const { label, seconds } = units.find(u => absDiff >= u.seconds) || { label: 'second', seconds: 1 }; const count = Math.floor(absDiff / seconds); const formatter = new Intl.RelativeTimeFormat(language, { numeric: 'auto' }); return formatter.format(isPast ? -count : count, label); }; export const getFormattedTime = _ref3 => { let { date, shouldShowSeconds = false } = _ref3; const { active: language } = getLanguage(); const timeOptions = { hour: '2-digit', minute: '2-digit', second: shouldShowSeconds ? '2-digit' : undefined }; const formattedTime = date.toLocaleTimeString(language, timeOptions).replace(/^0/, ''); const hourWord = getTimeString({ language }); return `${formattedTime} ${hourWord}`.trim(); }; export const getTimeString = _ref4 => { let { language } = _ref4; const map = { nl: 'uur', fr: 'heures', de: 'Uhr', es: 'horas', it: 'ore', pt: 'horas', pl: 'godzina', tr: 'saat', uk: 'година', en: '' }; return map[language ?? ''] ?? ''; }; //# sourceMappingURL=dateInfo.js.map