| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /**
- * 时间格式化工具函数
- * @param value 时间戳或日期字符串
- * @param format 格式化字符串,默认:YYYY-MM-DD HH:mm:ss
- * @returns 格式化后的时间字符串
- */
- export function changtime(value: number | string | Date, format: string = 'YYYY年MM月'): string {
- if (!value)
- return ''
- const date = new Date(value)
- // 检查日期是否有效
- if (Number.isNaN(date.getTime()))
- return ''
- const year = date.getFullYear()
- const month = String(date.getMonth() + 1).padStart(2, '0')
- const day = String(date.getDate()).padStart(2, '0')
- const hours = String(date.getHours()).padStart(2, '0')
- const minutes = String(date.getMinutes()).padStart(2, '0')
- const seconds = String(date.getSeconds()).padStart(2, '0')
- return format
- .replace('YYYY', String(year))
- .replace('MM', month)
- .replace('DD', day)
- .replace('HH', hours)
- .replace('mm', minutes)
- .replace('ss', seconds)
- }
- /**
- * 注册全局过滤器(Vue 3 兼容方式)
- * 在 main.ts 中调用此函数注册全局过滤器
- */
- export function registerGlobalFilters(app: any): void {
- // 注册全局属性,用于在模板中通过 $filters.changtime 使用
- app.config.globalProperties.$filters = {
- changtime
- }
- // 注册全局方法,使过滤器语法 | changtime 可以工作
- app.mixin({
- methods: {
- changtime
- }
- })
- }
|