vue---工具函数
工具函数
·
日期加减以及格式化为YY-MM-DD HH:MM:SS
main.js
import { createApp } from 'vue'
import App from './App.vue'
const dateChangeAndFormatted=(changeSeconds)=>{
const now = new Date();
let newDate = new Date(now.getTime()+changeSeconds)
const year = newDate.getFullYear();
const month = String(newDate.getMonth() + 1).padStart(2, '0');
const day = String(newDate.getDate()).padStart(2, '0');
const hours = String(newDate.getHours()).padStart(2, '0');
const minutes = String(newDate.getMinutes()).padStart(2, '0');
const seconds = String(newDate.getSeconds()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(formattedDate);
return formattedDate
}
const app = createApp(App)
app.provide("dateChangeAndFormatted",dateChangeAndFormatted)
.mount('#app')
子组件
setup() {
let dateChangeAndFormatted = inject("dateChangeAndFormatted")
return {formInline, moment, dateChangeAndFormatted}
},
这样就可以在子组件中使用了
this.dateChangeAndFormatted(0)
更多推荐
已为社区贡献1条内容
所有评论(0)