Add commit dates to apps

This commit is contained in:
Sam Carlton 2020-11-21 15:36:59 -06:00
parent 08fc82abfd
commit 25f5343b25
2 changed files with 97 additions and 2 deletions

View file

@ -0,0 +1,33 @@
// in miliseconds
const units = {
year : 24 * 60 * 60 * 1000 * 365,
month : 24 * 60 * 60 * 1000 * 365/12,
day : 24 * 60 * 60 * 1000,
hour : 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000
}
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
const getRelativeTime = function (d1, d2 = new Date()) {
var elapsed = d1 - d2
// "Math.abs" accounts for both "past" & "future" scenarios
for (var u in units)
if (Math.abs(elapsed) > units[u] || u == 'second')
return rtf.format(Math.round(elapsed/units[u]), u)
}
export default function (rawDate) {
const date = new Date(rawDate)
return {
date,
raw: rawDate,
timestamp: date.getTime(),
relative: getRelativeTime(date)
}
}