设备维修统计报表
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 通用格式化工具(后端返回的日期为 ISO 字符串,如 "2026-08-28T10:30:00")
|
||||
*/
|
||||
|
||||
// 补零:5 → "05"
|
||||
const pad = n => String(n).padStart(2, '0')
|
||||
|
||||
/**
|
||||
* 格式化为日期:2026-08-28
|
||||
* @param {string|Date} value 日期/时间值
|
||||
* @returns {string} 空值返回 ''
|
||||
*/
|
||||
export function fmtDate(value) {
|
||||
if (!value) return ''
|
||||
const d = new Date(value)
|
||||
if (isNaN(d.getTime())) return String(value).slice(0, 10)
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化为日期时间:2026-08-28 10:30:00
|
||||
* @param {string|Date} value 日期/时间值
|
||||
* @returns {string} 空值返回 ''
|
||||
*/
|
||||
export function fmtDateTime(value) {
|
||||
if (!value) return ''
|
||||
const d = new Date(value)
|
||||
if (isNaN(d.getTime())) return String(value).replace('T', ' ').slice(0, 19)
|
||||
return `${fmtDate(d)} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文件大小:1024 → "1.00 KB"
|
||||
* @param {number} bytes 字节数
|
||||
* @returns {string} 空值返回 ''
|
||||
*/
|
||||
export function fmtFileSize(bytes) {
|
||||
if (bytes === null || bytes === undefined || isNaN(bytes)) return ''
|
||||
if (bytes < 1024) return `${bytes} B`
|
||||
const units = ['KB', 'MB', 'GB', 'TB']
|
||||
let size = bytes
|
||||
let i = -1
|
||||
do {
|
||||
size /= 1024
|
||||
i++
|
||||
} while (size >= 1024 && i < units.length - 1)
|
||||
return `${size.toFixed(2)} ${units[i]}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断日期是否已超期(小于当前日期)
|
||||
* @param {string|Date} value 日期值
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isOverdue(value) {
|
||||
if (!value) return false
|
||||
const d = new Date(value)
|
||||
if (isNaN(d.getTime())) return false
|
||||
return d < new Date()
|
||||
}
|
||||
Reference in New Issue
Block a user