转换时间格式

export function timeRule(t, type) {
  if (!t) return;
  if (!t.includes("T")) {
    var date = new Date(t.replace(/\-/g, "/")); // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  } else {
    var date = new Date(t);
  }
  console.log("最近时间", date);
  var Y = date.getFullYear() + "-";
  var y = date.getFullYear();
  var M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  var mt =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  var d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var H =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  var Mi =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
    ":";
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); // 卡券时间
  if (type === "card") {
    //  2021年08月01日
    return y + "年" + mt + "月" + d + "日";
  } else if (type === "time") {
    // 2021-07-19 19:06:36
    return Y + M + D + H + Mi + s;
  } else if (type === "point") {
    // 2021.07.19
    return y + "." + mt + "." + d;
  } else {
    return Y + M + D;
  }
}

你可能感兴趣的:(转换时间格式)