2019-03-31 18:56:48 +00:00
|
|
|
/**
|
|
|
|
|
* Return the relative time betweeen a chosen moment and the current time
|
|
|
|
|
* @param previous : time we want to get difference
|
|
|
|
|
*/
|
|
|
|
|
export function timeDifference(previous) {
|
2019-08-23 14:48:16 +00:00
|
|
|
const current = new Date()
|
|
|
|
|
const msPerMinute = 60 * 1000
|
|
|
|
|
const msPerHour = msPerMinute * 60
|
|
|
|
|
const msPerDay = msPerHour * 24
|
|
|
|
|
const msPerWeek = msPerDay * 7
|
|
|
|
|
const elapsed = current - previous
|
2019-03-31 18:56:48 +00:00
|
|
|
|
|
|
|
|
if (elapsed < msPerMinute) {
|
2019-09-20 21:32:28 +00:00
|
|
|
return Math.round(elapsed / 1000) + ' seconds ago'
|
2019-03-31 18:56:48 +00:00
|
|
|
} else if (elapsed < msPerHour) {
|
2019-09-20 21:32:28 +00:00
|
|
|
return Math.round(elapsed / msPerMinute) + ' minutes ago'
|
2019-03-31 18:56:48 +00:00
|
|
|
} else if (elapsed < msPerDay) {
|
2019-09-20 21:32:28 +00:00
|
|
|
return Math.round(elapsed / msPerHour) + ' hours ago'
|
2019-03-31 18:56:48 +00:00
|
|
|
} else if (elapsed < msPerWeek) {
|
2019-09-20 21:32:28 +00:00
|
|
|
return Math.round(elapsed / msPerDay) + ' days ago'
|
2019-03-31 18:56:48 +00:00
|
|
|
} else {
|
2019-08-23 14:48:16 +00:00
|
|
|
const oldDate = new Date(previous)
|
|
|
|
|
const day = oldDate.getDate() < 10 ? '0' + oldDate.getDate() : oldDate.getDate()
|
|
|
|
|
const month = oldDate.getMonth() < 9 ? '0' + (oldDate.getMonth() + 1) : (oldDate.getMonth() + 1)
|
|
|
|
|
return day + '.' + month + '.' + oldDate.getFullYear().toString().substr(-2)
|
2019-03-31 18:56:48 +00:00
|
|
|
}
|
|
|
|
|
}
|