常用时间格式处理

传入的参数time是时间戳

//使用
const currentTime = new Date().getTime();
const current = this.timeStrFormat(currentTime);
console.log(current, 123);//2023-03-16 11:24:45
//时间生成并处理
    timeStrFormat(time) {
      // 获取方法
      const timeType = [
        "getFullYear",
        "getMonth",
        "getDate",
        "getHours",
        "getMinutes",
        "getSeconds",
      ];
      // 分隔符
      const separator = {
        getFullYear: "-",
        getMonth: "-",
        getDate: " ",
        getHours: ":",
        getMinutes: ":",
        getSeconds: "",
      };
      let resStr = "";
      for (let i = 0; i < timeType.length; i++) {
        const element = timeType[i];
        let resTime = new Date(time)[element]();
        // 获取月份的要+1
        resTime = element == "getMonth" ? resTime + 1 : resTime;
        // 小于10,前面加0
        resTime = resTime > 9 ? resTime : "0" + resTime;
        resStr = resStr + resTime + separator[element];
      }
      return resStr;
    },

你可能感兴趣的:(javascript,es6,Vue,javascript,前端,vue.js)