This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
scoresaber-reloadedv3/src/common/time-utils.ts

96 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-09-11 20:10:27 +01:00
/**
* This function returns the time ago of the input date
*
2024-10-01 11:52:28 +01:00
* @param input Date | number (timestamp)
2024-09-11 20:10:27 +01:00
* @returns the format of the time ago
*/
2024-10-01 11:52:28 +01:00
export function timeAgo(input: number) {
const inputDate = new Date(input).getTime(); // Convert input to a Date object if it's not already
const now = new Date().getTime();
const deltaSeconds = Math.floor((now - inputDate) / 1000); // Get time difference in seconds
if (deltaSeconds < 60) {
return "just now";
}
const timeUnits = [
{ unit: "y", seconds: 60 * 60 * 24 * 365 }, // years
{ unit: "mo", seconds: 60 * 60 * 24 * 30 }, // months
{ unit: "d", seconds: 60 * 60 * 24 }, // days
{ unit: "h", seconds: 60 * 60 }, // hours
{ unit: "m", seconds: 60 }, // minutes
];
let result = [];
let remainingSeconds = deltaSeconds;
for (let { unit, seconds } of timeUnits) {
const count = Math.floor(remainingSeconds / seconds);
if (count > 0) {
result.push(`${count}${unit}`);
remainingSeconds -= count * seconds;
2024-09-11 20:10:27 +01:00
}
2024-10-01 11:52:28 +01:00
// Stop after two units have been added
if (result.length === 2) break;
2024-09-11 20:10:27 +01:00
}
2024-10-01 11:52:28 +01:00
// Return formatted result with at most two units
return result.join(", ") + " ago";
2024-09-11 20:10:27 +01:00
}
2024-09-28 05:57:35 +01:00
2024-09-28 06:31:25 +01:00
/**
2024-09-28 06:37:10 +01:00
* Formats the date in the format "DD MMMM YYYY"
2024-09-28 06:31:25 +01:00
*
* @param date the date
*/
2024-09-28 06:37:10 +01:00
export function formatDateMinimal(date: Date) {
2024-09-28 06:31:25 +01:00
return date.toLocaleString("en-US", {
timeZone: "Europe/London",
2024-09-28 06:37:10 +01:00
day: "numeric",
month: "short",
year: "numeric",
2024-09-28 06:31:25 +01:00
});
}
2024-09-28 05:57:35 +01:00
/**
* Gets the midnight aligned date
*
* @param date the date
*/
export function getMidnightAlignedDate(date: Date) {
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
2024-09-28 05:57:35 +01:00
}
/**
* Gets the date X days ago
*
* @param days the number of days to go back
* @returns {Date} A Date object representing the date X days ago
*/
export function getDaysAgoDate(days: number): Date {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
/**
* Gets the amount of days ago a date was
*
* @param date the date
* @returns the amount of days
*/
export function getDaysAgo(date: Date): number {
const now = new Date();
const diffTime = Math.abs(now.getTime() - date.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24)) - 1;
}
/**
* Parses a date from a string
*
* @param date the date
*/
export function parseDate(date: string): Date {
return new Date(date);
}