@universe/util
Advanced tools
Comparing version
{ | ||
"name": "@universe/util", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"main": "dist/src/index.js", | ||
@@ -5,0 +5,0 @@ "description": "Universe development utilities.", |
@@ -245,1 +245,17 @@ // Generics type checking utils. | ||
} | ||
export function relativeDate(input: Date | number): string { | ||
const date = typeof input === 'number' ? new Date(input) : input; | ||
const diff = (((new Date()).getTime() - date.getTime()) / 1000); | ||
const dayDiff = Math.floor(diff / 86400); | ||
if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) { return date.toLocaleDateString('en-us', { year: 'numeric', month: 'short', day: 'numeric' }); } | ||
if (diff < 60) return 'just now'; | ||
if (diff < 120) return '1 minute ago'; | ||
if (diff < 3600) return Math.floor(diff / 60) + ' minutes ago'; | ||
if (diff < 7200) return '1 hour ago'; | ||
if (diff < 86400) return Math.floor(diff / 3600) + ' hours ago'; | ||
if (dayDiff === 1) return 'yesterday'; | ||
if (dayDiff < 7) return dayDiff + ' days ago'; | ||
if (dayDiff < 31) return Math.ceil(dayDiff / 7) + ' weeks ago'; | ||
return ''; | ||
} |
Sorry, the diff of this file is not supported yet
678
2.26%380766
-0.04%