export const timestampToTime = (timestamp, transLength) => {
  // 时间戳为10位需*1000，时间戳为13位不需乘1000
  let date = null;
  if (timestamp) {
    if (timestamp.length < 13) {
      date = new Date(timestamp * 1000);
    } else {
      date = new Date(timestamp);
    }
    let Y = date.getFullYear() + "-";
    let M =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1;
    let D =
      date.getDate() < 10 ? "-" + "0" + date.getDate() : "-" + date.getDate();
    let h =
      " " +
      (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
      ":";
    let m =
      (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
      ":";
    let s =
      date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    switch (transLength) {
      case 2:
        return Y + M;
      case 3:
        return Y + M + D;
      case 6:
        return Y + M + D + h + m + s;
    }
  } else {
    return "";
  }
};
// 小时：分钟 ==》 转分钟

export const transFormMinut = (String) => {
  if (typeof String === "number") {
    return;
  }
  if (!String) {
    return 0;
  }
  let arr = String.split(":");
  let hour =
    arr[0].indexOf("0") === 0 ? arr[0].substring(1, arr[0].length) : arr[0];
  let minu =
    arr[1].indexOf("0") === 0 ? arr[1].substring(1, arr[1].length) : arr[1];
  return hour * 60 + minu * 1;
  // return JSON.stringify(hour * 60 + minu * 1);
};
// // 分钟 ==》 小时
export const transFormTime = (num) => {
  if (typeof num != "string" && num !== 0) {
    return Math.floor(num / 60).toString() + ":" + (num % 60).toString();
  } else {
    return "0:00";
  }
};

//当前月第一天
export const getFirstDay = () => {
  let y = new Date().getFullYear(); //获取年份
  let m = new Date().getMonth() + 1; //获取月份
  let d = "01";
  m = m < 10 ? "0" + m : m; //月份补 0

  return [y, m, d].join("-");
};

//当前月最后一天
export const getLastDay = () => {
  let y = new Date().getFullYear(); //获取年份
  let m = new Date().getMonth() + 1; //获取月份
  let d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? "0" + m : m; //月份补 0
  d = d < 10 ? "0" + d : d; //日数补 0

  return [y, m, d].join("-");
};

//获取两日期之间日期列表函数
export const getdiffdate = (stime, etime) => {
  //初始化日期列表，数组
  let diffdate = new Array();
  let i = 0;
  //开始日期小于等于结束日期,并循环
  while (stime <= etime) {
    diffdate[i] = stime;
    //获取开始日期时间戳
    let stime_ts = new Date(stime).getTime();

    //增加一天时间戳后的日期
    let next_date = stime_ts + 24 * 60 * 60 * 1000;

    //拼接年月日，这里的月份会返回（0-11），所以要+1
    let next_dates_y = new Date(next_date).getFullYear() + "-";
    let next_dates_m =
      new Date(next_date).getMonth() + 1 < 10
        ? "0" + (new Date(next_date).getMonth() + 1) + "-"
        : new Date(next_date).getMonth() + 1 + "-";
    let next_dates_d =
      new Date(next_date).getDate() < 10
        ? "0" + new Date(next_date).getDate()
        : new Date(next_date).getDate();

    stime = next_dates_y + next_dates_m + next_dates_d;

    //增加数组key
    i++;
  }
  return diffdate;
};
// 获取某个日期是周几
export const getMyDay = (date) => {
  let week;
  if (date.getDay() == 0) week = "周日";
  if (date.getDay() == 1) week = "周一";
  if (date.getDay() == 2) week = "周二";
  if (date.getDay() == 3) week = "周三";
  if (date.getDay() == 4) week = "周四";
  if (date.getDay() == 5) week = "周五";
  if (date.getDay() == 6) week = "周六";
  return week;
};
// 求两个日期的 分钟差
export const getMinu = (s1, s2) => {
  var reDate = /\d{4}-\d{1,2}-\d{1,2} /;
  s1 = new Date((reDate.test(s1) ? s1 : "2023-01-01 " + s1).replace(/-/g, "/"));
  s2 = new Date((reDate.test(s2) ? s2 : "2023-01-01 " + s2).replace(/-/g, "/"));
  var ms = s2.getTime() - s1.getTime();
  if (ms < 0) return 0;
  return Math.floor(ms / 1000 / 60); //分钟
};
// 求两个日期的 秒差
export const getSec = (s1, s2) => {
  var reDate = /\d{4}-\d{1,2}-\d{1,2} /;
  s1 = new Date((reDate.test(s1) ? s1 : "2023-01-01 " + s1).replace(/-/g, "/"));
  s2 = new Date((reDate.test(s2) ? s2 : "2023-01-01 " + s2).replace(/-/g, "/"));
  var ms = s2.getTime() - s1.getTime();
  if (ms < 0) return 0;
  return Math.floor(ms / 1000); //秒
};
