From 6daa4493e251af97a45188f8b80d8f9a2d76887d Mon Sep 17 00:00:00 2001 From: RealOrangeOne Date: Fri, 11 Mar 2016 20:51:19 +0000 Subject: [PATCH] Add function to get time ago --- app/helpers/time-utils.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/helpers/time-utils.js diff --git a/app/helpers/time-utils.js b/app/helpers/time-utils.js new file mode 100644 index 0000000..90a8c32 --- /dev/null +++ b/app/helpers/time-utils.js @@ -0,0 +1,25 @@ +export function timeSince(date) { + const seconds = Math.floor((new Date() - date) / 1000); + + let interval = Math.floor(seconds / 31536000); + if (interval > 1) { + return interval + ' years'; + } + interval = Math.floor(seconds / 2592000); + if (interval > 1) { + return interval + ' months'; + } + interval = Math.floor(seconds / 86400); + if (interval > 1) { + return interval + ' days'; + } + interval = Math.floor(seconds / 3600); + if (interval > 1) { + return interval + ' hours'; + } + interval = Math.floor(seconds / 60); + if (interval > 1) { + return interval + ' minutes'; + } + return Math.floor(seconds) + ' seconds'; +}