在一个单独的文件中新建一个关于日期格式化的文件,js/ts
export const dateTime = (e: any) => {
let time = new Date(e)
let Y = time.getFullYear()
let M = time.getMonth() + 1
let D = time.getDate()
let h = time.getHours()
let m = time.getMinutes()
let s = time.getSeconds()
return Y + '-' + M + '-' + D + ' ' + h + ":" + m + ":" + s
}
然后再需要的页面引入该函数即可直接使用。
import { dateTime } from "@/utils/time"
const time = new Date()
console.log(dateTime(time));
注:export导出的函数或方法,需要在引入时加{},如果是export default导出的则直接写导出的名字就可以,不需要加{}。因为export default 一个文件中只能有一个,且不能和export一起使用。