aboutsummaryrefslogtreecommitdiff
path: root/src/utils/moments.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-10-22 15:34:21 -0700
committerGitHub <noreply@github.com>2020-10-22 18:34:21 -0400
commitd0237cbb61e5c4d77c7b0cefc50891639646ee91 (patch)
tree5b0c1e33c1043887ad45c06a30173dc469d28228 /src/utils/moments.ts
parent5db451725d6165de16ee11cda608a05e96e481f9 (diff)
[TMA 236] Comments PR (#64)
* Added comments count and retrieve comments * Working draft * The one before cleanup * Finally * Added time icon and major refactoring * Small fix for social media taggs * Addressed review comments
Diffstat (limited to 'src/utils/moments.ts')
-rw-r--r--src/utils/moments.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/utils/moments.ts b/src/utils/moments.ts
new file mode 100644
index 00000000..0f8021cb
--- /dev/null
+++ b/src/utils/moments.ts
@@ -0,0 +1,33 @@
+import moment from 'moment';
+
+//A util that calculates the difference between a given time and current time
+//Returns the difference in the largest possible unit of time (days > hours > minutes > seconds)
+
+export const getTimePosted = (date_time: string) => {
+ const datePosted = moment(date_time);
+ const now = moment();
+ var time = date_time;
+ var difference = now.diff(datePosted, 'seconds');
+
+ //Creating elapsedTime string to display to user
+ // 0 to less than 1 minute
+ if (difference < 60) {
+ time = difference + ' seconds';
+ }
+ // 1 minute to less than 1 hour
+ else if (difference >= 60 && difference < 60 * 60) {
+ difference = now.diff(datePosted, 'minutes');
+ time = difference + (difference === 1 ? ' minute' : ' minutes');
+ }
+ //1 hour to less than 1 day
+ else if (difference >= 60 * 60 && difference < 24 * 60 * 60) {
+ difference = now.diff(datePosted, 'hours');
+ time = difference + (difference === 1 ? ' hour' : ' hours');
+ }
+ //1 day to less than 7 days
+ else if (difference >= 24 * 60 * 60 && difference < 7 * 24 * 60 * 60) {
+ difference = now.diff(datePosted, 'days');
+ time = difference + (difference === 1 ? ' day' : ' days');
+ }
+ return time;
+};