Created
September 2, 2016 13:35
-
-
Save lnmunhoz/9f1ab73fc80fd805d6f7a0e41a5c7c41 to your computer and use it in GitHub Desktop.
Moment fromNow methods. Minimalist implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Lucas Nogueira Munhoz | |
* Start: 08:40AM - End: 10:03AM | |
*/ | |
// units.js | |
const units = { second: 1000 }; | |
units.minute = 60 * units.second; | |
units.hour = 60 * units.minute; | |
units.day = 24 * units.hour; | |
units.week = 7 * units.day; | |
units.month = 4 * units.week; | |
units.year = 12 * units.month | |
// units_range.js | |
const unitsRange = { | |
second: { | |
min: units.second, | |
max: units.second * 59 | |
} | |
} | |
unitsRange.minute = { | |
min: units.minute, | |
max: units.minute * 59 + unitsRange.second.max | |
} | |
unitsRange.hour = { | |
min: units.hour, | |
max: units.hour * 23 + unitsRange.minute.max | |
} | |
unitsRange.day = { | |
min: units.day, | |
max: units.day * 6 + unitsRange.hour.max | |
} | |
unitsRange.week = { | |
min: units.week, | |
max: units.week * 3 + unitsRange.day.max | |
} | |
unitsRange.month = { | |
min: units.month, | |
max: units.month * 11 + unitsRange.week.max | |
} | |
// fromNow.js | |
const toHumanTime = (timestamp, unit) => { | |
return Math.floor(timestamp / unit); | |
} | |
const toHumanString = (time, unit) => { | |
const humanTime = toHumanTime(time, units[unit]) | |
return humanTime === 1 | |
? `${humanTime} ${unit} ago` | |
: `${humanTime} ${unit}s ago` | |
} | |
const fromNow = timestamp => { | |
const time = new Date().getTime() - new Date(timestamp).getTime() | |
for (var unit in unitsRange) { | |
if (time < units.second) return 'now'; | |
if (unitsRange[unit].min <= time && unitsRange[unit].max >= time ) { | |
return toHumanString(time, unit); | |
} | |
} | |
return toHumanString(time, 'year') | |
} | |
// -------------------------------------------- | |
// ------- Testes ----------------------------- | |
// -------------------------------------------- | |
// menos de 1 segundo atrás | |
console.log(fromNow(new Date().getTime())) | |
// 1 segundo atrás | |
console.log(fromNow(new Date().getTime() - units.second)) | |
// 23 segundos atrás | |
console.log(fromNow(new Date().getTime() - units.second * 23)) | |
// 1 minuto atrás | |
console.log(fromNow(new Date().getTime() - units.minute)) | |
// 54 minutos atrás | |
console.log(fromNow(new Date().getTime() - units.minute * 54)) | |
// 1 dia atrás | |
console.log(fromNow(new Date().getTime() - units.day)) | |
// 6 dias atrás | |
console.log(fromNow(new Date().getTime() - units.day * 6)) | |
// 2 semanas atrás | |
console.log(fromNow(new Date().getTime() - units.week * 2)) | |
// 3 meses atrás | |
console.log(fromNow(new Date().getTime() - units.month * 3)) | |
// 4 anos atrás | |
console.log(fromNow(new Date().getTime() - units.year * 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment