@chayns-components/date
Version:
A set of beautiful React components for developing your own applications with chayns.
140 lines • 3.59 kB
JavaScript
import { getLanguage, Language } from 'chayns-api';
import { isCurrentYear, isToday, isTomorrow, isYesterday } from './date';
export const getDateInfo = ({
date,
language,
shouldShowYear,
shouldShowTime,
shouldShowOnlyTime,
shouldShowDayOfWeek,
shouldShowRelativeDayOfWeek,
shouldUseShortText
}) => {
const {
active: activeLanguage
} = getLanguage();
const timeParts = {};
if (shouldShowTime || shouldShowOnlyTime) {
timeParts.hour = '2-digit';
timeParts.minute = '2-digit';
}
let formattedTime = '';
if (Object.keys(timeParts).length > 0) {
formattedTime = `${shouldShowOnlyTime ? '' : ', '}${date.toLocaleTimeString(language ?? activeLanguage, {
...timeParts
})}`;
}
const hourWord = getTimeString({
language: language ?? activeLanguage
});
formattedTime += shouldShowTime || shouldShowOnlyTime ? ` ${hourWord}` : '';
if (shouldShowOnlyTime) {
return formattedTime;
}
let dayPart = '';
if (shouldShowRelativeDayOfWeek) {
const rtf = new Intl.RelativeTimeFormat(language ?? activeLanguage, {
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 ?? activeLanguage, {
weekday: shouldUseShortText ? 'short' : 'long'
});
}
const dateParts = {
day: '2-digit',
month: shouldUseShortText ? 'short' : 'long'
};
if (shouldShowYear && !isCurrentYear(date)) {
dateParts.year = 'numeric';
}
const formattedDate = `${date.toLocaleDateString(language ?? activeLanguage, dateParts)}${formattedTime}`;
return `${dayPart}${dayPart ? ', ' : ''}${formattedDate}`;
};
const capitalizeFirstLetter = text => text.charAt(0).toUpperCase() + text.slice(1);
export const getTimeTillNow = ({
date,
currentDate,
language = Language.English
}) => {
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 = ({
date,
shouldShowSeconds = false
}) => {
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 = ({
language
}) => {
const map = {
nl: '',
fr: '',
de: 'Uhr',
es: '',
it: '',
pt: '',
pl: '',
tr: '',
uk: '',
en: ''
};
return map[language ?? ''] ?? '';
};
//# sourceMappingURL=dateInfo.js.map