21 lines
619 B
TypeScript
21 lines
619 B
TypeScript
import { format } from 'date-fns';
|
|
|
|
export const formatMemory = (bytes: number | string) => {
|
|
bytes = typeof bytes === 'string' ? parseInt(bytes, 10) : bytes;
|
|
const units = ['B', 'KB', 'MB', 'GB'];
|
|
let size = bytes;
|
|
let unitIndex = 0;
|
|
while (size >= 1024 && unitIndex < units.length - 1) {
|
|
size /= 1024;
|
|
unitIndex++;
|
|
}
|
|
return `${size.toFixed(1)} ${units[unitIndex]}`;
|
|
};
|
|
|
|
export const formatDate = (date: string | Date, type: 'full' | 'short' = 'full') => {
|
|
if (type === 'short') {
|
|
return format(new Date(date), 'MM-dd HH:mm');
|
|
}
|
|
return format(new Date(date), 'yyyy-MM-dd HH:mm:ss');
|
|
};
|