Files
developer-roadmap/src/lib/date.ts
T
Arik ChakmaandKamran Ahmed a913da47a7 feat: implement dashboard page (#6965)
* wip: implement success modal

* feat: share solution modal

* fix: step count issue

* fix: responsiveness share button

* feat: project listing

* wip

* wip: project status

* feat: personal dashboard

* wip: team activity

* feat: personal dashboard page

* feat: add team member tooltip

* feat: dashboard favourite

* fix: invite team page

* fix: invite team

* wip: update design

* fix: add custom roadmaps

* feat: add projects in public page

* wip: dashboard re-design

* feat: add teams

* feat: update dashboard design

* feat: update dashboard design

* feat: add streak stats

* feat: add topics done today count

* UI changes for dashboard

* Refactor progress stack

* Progress stack UI

* Progress stack card fixes

* Update card designs

* AI and custom roadmap

* Update recommendation

* Update recommendation UI

* Add AI roadmap listing

* Redirect to team page from dashboard

---------

Co-authored-by: Kamran Ahmed <kamranahmed.se@gmail.com>
2024-09-11 16:01:26 +01:00

80 lines
2.0 KiB
TypeScript

import dayjs from 'dayjs';
export function getRelativeTimeString(
date: string | Date,
isTimed: boolean = false,
): string {
if (!Intl?.RelativeTimeFormat) {
return date.toString();
}
const rtf = new Intl.RelativeTimeFormat('en', {
numeric: 'auto',
style: 'narrow',
});
const currentDate = new Date();
const targetDate = new Date(date);
const diffInMilliseconds = currentDate.getTime() - targetDate.getTime();
const diffInMinutes = Math.round(diffInMilliseconds / (1000 * 60));
const diffInHours = Math.round(diffInMilliseconds / (1000 * 60 * 60));
const diffInDays = Math.round(diffInMilliseconds / (1000 * 60 * 60 * 24));
let relativeTime;
if (diffInMinutes < 60) {
relativeTime = rtf.format(-diffInMinutes, 'minute');
} else if (diffInHours < 24) {
relativeTime = rtf.format(-diffInHours, 'hour');
} else if (diffInDays < 7) {
if (isTimed) {
relativeTime = dayjs(date).format('ddd h:mm A');
} else {
relativeTime = rtf.format(-diffInDays, 'day');
}
} else if (diffInDays < 30) {
relativeTime = rtf.format(-Math.round(diffInDays / 7), 'week');
} else if (diffInDays < 365) {
relativeTime = rtf.format(-Math.round(diffInDays / 30), 'month');
} else {
if (isTimed) {
relativeTime = dayjs(date).format('MMM D, YYYY h:mm A');
} else {
relativeTime = dayjs(date).format('MMM D, YYYY');
}
}
if (relativeTime === 'this minute') {
return 'just now';
}
return relativeTime;
}
export function formatMonthDate(date: string): string {
return new Date(date).toLocaleDateString('en-US', {
month: 'long',
year: 'numeric',
});
}
export function formatActivityDate(date: string): string {
return new Date(date).toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
});
}
export function getCurrentPeriod() {
const now = new Date();
const hour = now.getHours();
if (hour < 12) {
return 'morning';
} else if (hour < 18) {
return 'afternoon';
} else {
return 'evening';
}
}