add typescript support in svelte files, .js to .ts
This commit is contained in:
33
src/lib/helper/timeElapsed.ts
Normal file
33
src/lib/helper/timeElapsed.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
const MINUTE = 60;
|
||||
const HOUR = 60 * MINUTE;
|
||||
const DAY = 24 * HOUR;
|
||||
const YEAR = 365 * DAY;
|
||||
const MONTH = YEAR / 12;
|
||||
|
||||
/**
|
||||
* get readable string of time elapsed since date
|
||||
* @param {Date} date
|
||||
* @returns string
|
||||
*/
|
||||
export default function timeElapsed(date) {
|
||||
const now = new Date();
|
||||
const age = Math.floor((now.getTime() - date.getTime()) / 1000);
|
||||
|
||||
const years = Math.floor(age / YEAR);
|
||||
let remainder = age % YEAR;
|
||||
const months = Math.floor(remainder / MONTH);
|
||||
remainder %= MONTH;
|
||||
const days = Math.floor(remainder / DAY);
|
||||
remainder %= DAY;
|
||||
const hours = Math.floor(remainder / HOUR);
|
||||
remainder %= HOUR;
|
||||
const minutes = Math.floor(remainder / MINUTE);
|
||||
const seconds = remainder % MINUTE;
|
||||
if (years > 0) return years === 1 ? 'vor 1 Jahr' : `vor ${years} Jahren`;
|
||||
if (months > 0) return months === 1 ? 'vor 1 Monat' : `vor ${months} Monaten`;
|
||||
if (days > 0) return days === 1 ? 'vor 1 Tag' : `vor ${days} Tagen`;
|
||||
if (hours > 0) return hours === 1 ? 'vor 1 Stunde' : `vor ${hours} Stunden`;
|
||||
if (minutes > 0) return minutes === 1 ? 'vor 1 Minute' : `vor ${minutes} Minuten`;
|
||||
|
||||
return seconds === 1 ? 'vor 1 Sekunde' : `vor ${seconds} Sekunden`;
|
||||
}
|
||||
Reference in New Issue
Block a user