-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
querwurzel
committed
Nov 1, 2023
1 parent
98c231b
commit 1d9c74a
Showing
1 changed file
with
33 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,84 @@ | ||
|
||
export const toDateTimeString = (date: string | Date): string => { | ||
return date | ||
? toTimeString(date) + ' / ' + toDateString(date) | ||
: null; | ||
if (!date) { | ||
return null; | ||
} | ||
|
||
const instant = toDate(date); | ||
return toTimeString(instant) + ' / ' + toDateString(instant); | ||
} | ||
|
||
export const toDateString = (date: string | Date): string => { | ||
if (!date) { | ||
return null; | ||
} | ||
|
||
const instance = date instanceof Date ? date : toDate(date); | ||
return instance.toLocaleDateString(navigator.language || 'en', {day: '2-digit', month: 'long', year: 'numeric'}); | ||
const instant = toDate(date); | ||
return instant.toLocaleDateString(navigator.language || 'en', {day: '2-digit', month: 'long', year: 'numeric'}); | ||
} | ||
|
||
export const toTimeString = (date: string | Date): string => { | ||
if (!date) { | ||
return null; | ||
} | ||
|
||
const instance = date instanceof Date ? date : toDate(date); | ||
return instance.toLocaleTimeString(navigator.language || 'en', {hour: '2-digit', minute: '2-digit'}); | ||
const instant = toDate(date); | ||
return instant.toLocaleTimeString(navigator.language || 'en', {hour: '2-digit', minute: '2-digit'}); | ||
} | ||
|
||
export const relativeDiffLabel = (date: string | Date): string => { | ||
if (!date) { | ||
return null; | ||
} | ||
|
||
const instant = date instanceof Date ? date : toDate(date); | ||
const instant = toDate(date); | ||
const now = new Date(); | ||
const diff = Math.abs(Math.trunc((now.getTime() - instant.getTime()) / 1000)); // seconds | ||
const diffSeconds = Math.abs(Math.trunc((now.getTime() - instant.getTime()) / 1000)); | ||
|
||
if (diff > 2419200) { // > 4 weeks: 60 * 60 * 24 * 7 * 4 | ||
if (diffSeconds > 2419200) { // > 4 weeks: 60 * 60 * 24 * 7 * 4 | ||
return toDateString(instant); | ||
} | ||
|
||
if (diff <= 1) { | ||
if (diffSeconds <= 1) { | ||
return "1 second ago" | ||
} | ||
if (diff < 60) { // < minute | ||
return diff + " seconds ago"; | ||
if (diffSeconds < 60) { // < minute | ||
return diffSeconds + " seconds ago"; | ||
} | ||
|
||
if (diff < 120) { // < 2 minutes: 60 * 2 | ||
if (diffSeconds < 120) { // < 2 minutes: 60 * 2 | ||
return "1 minute ago" | ||
} | ||
if (diff < 3600) { // < hour: 60 * 60 | ||
return Math.trunc(diff / 60) + " minutes ago"; | ||
if (diffSeconds < 3600) { // < hour: 60 * 60 | ||
return Math.trunc(diffSeconds / 60) + " minutes ago"; | ||
} | ||
|
||
if (diff < 7200) { // < 2 hours: (60 * 60) * 2 | ||
if (diffSeconds < 7200) { // < 2 hours: (60 * 60) * 2 | ||
return '1 hour ago'; | ||
} | ||
if (diff < 86400) { // < day: (60 * 60) * 24 | ||
return Math.trunc(diff / 3600) + ' hours ago'; | ||
if (diffSeconds < 86400) { // < day: (60 * 60) * 24 | ||
return Math.trunc(diffSeconds / 3600) + ' hours ago'; | ||
} | ||
|
||
if (diff < 172800) { // < 2 days: (60 * 60 * 24) * 2 | ||
if (diffSeconds < 172800) { // < 2 days: (60 * 60 * 24) * 2 | ||
return '1 day ago'; | ||
} | ||
if (diff < 604800) { // < week: (60 * 60 * 24) * 7 | ||
return Math.trunc(diff / 86400) + ' days ago'; | ||
if (diffSeconds < 604800) { // < week: (60 * 60 * 24) * 7 | ||
return Math.trunc(diffSeconds / 86400) + ' days ago'; | ||
} | ||
|
||
if (diff < 1209600) { // < 2 weeks: (60 * 60 * 24 * 7) * 2 | ||
if (diffSeconds < 1209600) { // < 2 weeks: (60 * 60 * 24 * 7) * 2 | ||
return '1 week ago'; | ||
} | ||
if (diff < 2419200) { // < 4 weeks: (60 * 60 * 24 * 7) * 4 | ||
return Math.trunc(diff / 604800) + ' weeks ago'; | ||
if (diffSeconds < 2419200) { // < 4 weeks: (60 * 60 * 24 * 7) * 4 | ||
return Math.trunc(diffSeconds / 604800) + ' weeks ago'; | ||
} | ||
}; | ||
|
||
const toDate = (date: string): Date => { | ||
const toDate = (date: string | Date): Date => { | ||
if (date instanceof Date) { | ||
return date; | ||
} | ||
|
||
return new Date(Date.parse(date)); | ||
} |