aboutsummaryrefslogtreecommitdiff
path: root/src/utils/moments.ts
diff options
context:
space:
mode:
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;
+};