40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
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 `${Number.parseFloat((bytes / k ** i).toFixed(decimals))} ${sizes[i]}`
|
|
}
|
|
|
|
export function isAmount(str: any) {
|
|
const amountRegex = /^\d+(\.\d{1,2})?$/
|
|
return amountRegex.test(str)
|
|
}
|
|
/**
|
|
* 金额转换千分位
|
|
* @param amount 金额数值
|
|
* @returns 格式化后的金额字符串
|
|
*/
|
|
export function formatAmount(amount: number | undefined): string {
|
|
if (amount === undefined || amount === null)
|
|
return '-'
|
|
return amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
}
|
|
|
|
// function isAmount(str) {
|
|
// // 正则表达式
|
|
|
|
// }
|