vue中时间过滤器中的月日不足两位补0

参考文档:https://blog.csdn.net/qq_51075057/article/details/115123324

 

vue中时间过滤器中的月日不足两位补0

先将月,和日转换为字符串,然后用字符串的padStrat(2,0)方法(两位,不足补0)

filters: {
    handleData(date) {
      const timeDate = new Date(date);
      const year = timeDate.getFullYear();
      const month = (timeDate.getMonth() + 1).toString().padStart(2, 0);
      const day = timeDate.getDate().toString().padStart(2, 0);
      const hour = timeDate.getHours().toString().padStart(2, 0);
      const min = timeDate.getMinutes().toString().padStart(2, 0);
      const s = timeDate.getSeconds().toString().padStart(2, 0);
      return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + s;
    },
        },

 

posted @ 2024-03-30 09:54  小小仓鼠  阅读(33)  评论(0编辑  收藏  举报