不使用框架的方法,手动处理时间设为年月日格式,不需要时分秒


方案一:使用 formatter 属性(推荐)

<el-table-column 
  prop="time" 
  label="保质期" 
  align="center" 
  width="160" 
  :show-overflow-tooltip="true"
  :formatter="dateFormat">
</el-table-column>

并在 methods 中添加格式化方法:

methods: {
  // 添加日期格式化方法
  dateFormat(row, column, cellValue) {
    if (!cellValue) return '';
    const date = new Date(cellValue);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  },
  // 其他方法...
}

方案二:使用 template 和过滤器

<el-table-column 
  label="保质期" 
  align="center" 
  width="160" 
  :show-overflow-tooltip="true">
  <template slot-scope="scope">
    {{ scope.row.time| dateFormat }}
  </template>
</el-table-column>

并在 filters 中添加过滤器:

filters: {
  dateFormat(value) {
    if (!value) return '';
    const date = new Date(value);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  }
}

这里推荐使用方案一,因为它更符合 Element UI 的设计模式,代码也更简洁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值