From fc8af866f7cf4ffa63fa3b2bd78cf2fe9eddb846 Mon Sep 17 00:00:00 2001 From: harshtandiya Date: Wed, 23 Oct 2024 00:38:46 +0530 Subject: [PATCH] feat: add `getRelativeTime` method --- frontend/src/utils/helpers.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/frontend/src/utils/helpers.js b/frontend/src/utils/helpers.js index eeda556..61b379c 100644 --- a/frontend/src/utils/helpers.js +++ b/frontend/src/utils/helpers.js @@ -1,3 +1,12 @@ +import dayjs from 'dayjs' +import relativeTime from 'dayjs/plugin/relativeTime' +import isToday from 'dayjs/plugin/isToday' +import isYesterday from 'dayjs/plugin/isYesterday' + +dayjs.extend(relativeTime) +dayjs.extend(isToday) +dayjs.extend(isYesterday) + export const getRedirectUrl = (route) => { return window.location.origin + '/' + route } @@ -5,3 +14,14 @@ export const getRedirectUrl = (route) => { export const generateRandomId = () => { return Math.random().toString(36).slice(2, 8) } + +export const getRelativeTime = (datetime) => { + const date = dayjs(datetime) + if (date.isToday()) { + return 'Today' + } else if (date.isYesterday()) { + return 'Yesterday' + } else { + return date.fromNow() + } +}