@atlaskit/profilecard
Version:
A React component to display a card with user information.
29 lines • 899 B
JavaScript
import differenceInMonths from 'date-fns/differenceInMonths';
import isThisMonth from 'date-fns/isThisMonth';
import isThisWeek from 'date-fns/isThisWeek';
import isValid from 'date-fns/isValid';
export function isValidDate(date, today = new Date()) {
return !!date.getTime && isValid(date) && date.getTime() <= today.getTime();
}
export default function getRelativeDateKey(date, today = new Date()) {
if (!date || !isValidDate(date, today)) {
return null;
}
if (isThisWeek(date)) {
return 'ThisWeek';
}
if (isThisMonth(date)) {
return 'ThisMonth';
}
if (date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth() - 1) {
return 'LastMonth';
}
const diffInMonths = differenceInMonths(today, date);
if (diffInMonths < 6) {
return 'AFewMonths';
}
if (diffInMonths <= 12) {
return 'SeveralMonths';
}
return 'MoreThanAYear';
}