1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-27 19:36:24 -07:00
gochan/frontend/ts/formatting.ts
2023-05-23 12:32:46 -07:00

22 lines
No EOL
672 B
TypeScript

/**
* Formats the timestamp strings from JSON into a more readable format
* @param dateStr timestamp string, assumed to be in ISO Date-Time format
*/
export function formatDateString(dateStr: string) {
const date = new Date(dateStr);
return date.toDateString() + ", " + date.toLocaleTimeString();
}
/**
* Formats the given number of bytes into an easier to read filesize
*/
export function formatFileSize(size: number) {
if(size < 1000) {
return `${size} B`;
} else if(size <= 100000) {
return `${(size/1024).toFixed(1)} KB`;
} else if(size <= 100000000) {
return `${(size/1024/1024).toFixed(2)} MB`;
}
return `${(size/1024/1024/1024).toFixed(2)} GB`;
}