Date前端日期变为年龄处理方法

现在页面展示效果为:

 1,首先编写一个过滤器用来处理想要处理的数据

 2,将要处理的数据绑定到过滤器上

3,过滤器中:输入该代码即可完成效果(重点)

 filters: {
    showAge(value) {
      // 指定日期
      let birthdays = new Date(value.replace(/-/g, "/"))
      // 当前系统日期
      let d = new Date();
      let age = d.getFullYear() - birthdays.getFullYear() -  (d.getMonth() < birthdays.getMonth() || (d.getMonth() == birthdays.getMonth() && d.getDate() < birthdays.getDate()) ? 1 : 0);
      return age
    }
  },

 4,效果

猜你喜欢

转载自blog.csdn.net/m0_64550837/article/details/127137439