工具方法
创建工具文件:util.ts
/*** 获取时间并格式化函数* @param M 格式模板 如: YYYY-MM-DD ...* @param Time 可选传入时间参数 默认为 Now*/
export const getFormatDate = (M: string, Time: Date | null | string | number = null) => {let date: Date = Time ? new Date(Time) : new Date();let pattern: RegExp = /(Y{2,4}|M{1,2}|D{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|C{1,2}|W{1,2})/g;let tmp: any = (S: string | number) => (Number(S) < 10 ? "0" + S : S);let res: string = M.replace(pattern, ($0: string) => {switch ($0) {case "YY":return date.getFullYear().toString().slice(-2);case "YYYY":return date.getFullYear();case "M":return date.getMonth() + 1;case "MM":return tmp(date.getMonth() + 1);case "D":return date.getDate();case "DD":return tmp(date.getDate());case "W":return date.getDay();case "WW":return ["日", "一", "二", "三", "四", "五", "六"][date.getDay()];case "H":return date.getHours();case "HH":return tmp(date.getHours());case "m":return date.getMinutes();case "mm":return tmp(date.getMinutes());case "s":return date.getSeconds();case "ss":return tmp(date.getSeconds());case "C":return Math.trunc(date.getTime() / 1000);case "CC":return date.getTime();default:return $0;}});return res;
};
测试
index.vue
<script setup lang="ts">
import { getFormatDate } from "@/utils/util";console.log(getFormatDate("YYYY-MM-DD HH:mm:ss"));
</script>