diff options
| author | ankit-thanekar007 <ankit.thanekar007@gmail.com> | 2021-05-05 11:53:22 -0700 |
|---|---|---|
| committer | ankit-thanekar007 <ankit.thanekar007@gmail.com> | 2021-05-05 11:56:09 -0700 |
| commit | 9ae27c5717bace27a2db2c8dab1241c8a6e3da25 (patch) | |
| tree | 4729387d8767baef7606403644e5e5ec79cc10e1 /src/utils | |
| parent | 8c1fb85de4e54bc8cb95587962088e8b15a60f94 (diff) | |
Added timestamps on notification
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/moments.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/utils/moments.ts b/src/utils/moments.ts index 87f062af..12323e7b 100644 --- a/src/utils/moments.ts +++ b/src/utils/moments.ts @@ -37,3 +37,41 @@ export const getTimePosted = (date_time: string) => { } return time; }; + +export const getTimeInShorthand = (date_time: string) => { + //TODO: Explore this function to get dates in our range and format + // const datePosted = moment(date_time); + // return moment(datePosted).fromNow(); + + const datePosted = moment(date_time); + const now = moment(); + var time = date_time; + var difference = now.diff(datePosted, 's'); + + // Creating elapsedTime string to display to user + // 0 to less than 1 minute + if (difference < 60) { + time = difference + 's'; + } + // 1 minute to less than 1 hour + else if (difference >= 60 && difference < 60 * 60) { + difference = now.diff(datePosted, 'm'); + time = difference + 'm'; + } + // 1 hour to less than 1 day + else if (difference >= 60 * 60 && difference < 24 * 60 * 60) { + difference = now.diff(datePosted, 'h'); + time = difference + 'h'; + } + // Any number of days + else if (difference >= 24 * 60 * 60 && difference < 24 * 60 * 60 * 7) { + difference = now.diff(datePosted, 'd'); + time = difference + 'd'; + } + // More than 7 days + else if (difference >= 24 * 60 * 60 * 7) { + difference = now.diff(datePosted, 'w'); + time = difference + 'w'; + } + return time; +}; |
