diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/index.ts | 1 | ||||
-rw-r--r-- | src/utils/moments.ts | 33 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts index 5bc168e3..a7e45979 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './screenDimensions'; export * from './statusBarHeight'; +export * from './moments'; 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; +}; |