日期加减以及格式化为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)
Logo

前往低代码交流专区

更多推荐