可以使用 moment.js 库来进行时间格式化,将时间值格式化为所需的格式。然后,将格式化后的时间值拼接在一起,渲染到 table 上。

首先,确保已经引入了 moment.js 库。

然后,使用 moment 函数将 startAccountingTime 和 endAccountingTime 进行格式化,格式化为'YYYY年MM月'的形式。

import moment from 'moment';

// ...

data() {
  return {
    startAccountingTime: '2023-08-29',
    endAccountingTime: '2023-09-30'
  }
},
computed: {
  formattedStartAccountingTime() {
    return moment(this.startAccountingTime).format('YYYY年MM月');
  },
  formattedEndAccountingTime() {
    return moment(this.endAccountingTime).format('YYYY年MM月');
  }
}

然后,在 table 的 el-table-column 中使用 formattedStartAccountingTime 和 formattedEndAccountingTime 进行渲染。

<el-table-column label="会计期间" align="center" width="220">
  <template slot-scope="scope">
    {{ formattedStartAccountingTime }} - {{ formattedEndAccountingTime }}
  </template>
</el-table-column>

这样,会计期间就会以“2023年09月 - 2023年09月”的形式渲染在 table 上了。

Vue.js 中使用 moment.js 格式化时间并渲染到表格

原文地址: https://www.cveoy.top/t/topic/qx8h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录