mcwl-pc/app/utils/index.ts

28 lines
873 B
TypeScript

export async function formatDate(timestamp: string) {
const date = new Date(timestamp)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
export function formatFileSize(bytes:number = 0, decimals = 2) {
if (bytes === 0) return '0 B';
const k = 1024; // 1 KB = 1024 B
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k)); // 计算单位索引
// 转换为合适的单位并保留指定小数位数
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
}
export function isAmount(str:any) {
const amountRegex = /^\d+(\.\d{1,2})?$/;
return amountRegex.test(str);
}
// function isAmount(str) {
// // 正则表达式
// }