/**
 * @description  格式化时间戳
 * @param {String} format  YYYY-MM-DD hh:mm:ss
 * @param {String} daytime 时间戳，单位为毫秒
 * @returns 返回格式化后的时间字符串
 */

export function formatTime(daytime, format = 'YYYY-MM-DD hh:mm:ss') {
  const date = new Date(daytime);
  const year = date.getFullYear();
  const month = ('0' + (date.getMonth() + 1)).slice(-2);
  const day = ('0' + date.getDate()).slice(-2);
  const hours = ('0' + date.getHours()).slice(-2);
  const minutes = ('0' + date.getMinutes()).slice(-2);
  const seconds = ('0' + date.getSeconds()).slice(-2);
  const map = {
    YYYY: String(year),
    MM: month,
    DD: day,
    hh: hours,
    mm: minutes,
    ss: seconds,
  };

  return format.replace(/YYYY|MM|DD|hh|mm|ss/g, matched => map[matched]);
}

export function formatTime1(timestamp) {
  const date = new Date(timestamp * 1000); // 转换为毫秒
  const year = date.getFullYear();
  const month = ('0' + (date.getMonth() + 1)).slice(-2);
  const day = ('0' + date.getDate()).slice(-2);
  const hour = ('0' + date.getHours()).slice(-2);
  const minute = ('0' + date.getMinutes()).slice(-2);
  const second = ('0' + date.getSeconds()).slice(-2);
  
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
/**
 * @param {*} endTime
 * @returns
 */

export function intervalTime(endTime) {
  // var timestamp=new Date().getTime(); //计算当前时间戳
  var date1 = new Date().getTime(); //计算当前时间戳 (毫秒级)

  var date2 = endTime; //结束时间
  // var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
  var date3 = (date1 - date2) * 1000; //时间差的毫秒数
  //计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000));
  if (days < 0) {
    days = days * -1;
  }
  //计算出小时数
  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  //计算相差秒数
  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  console.log(days + '天 ' + hours + '小时 ');
  // return   days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
  return days + '天 ' + hours + '小时 ';
}

/**
 * @param {*} e
 * @returns
 */

export function changeTime(e) {
  let time = new Date(e);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (date < 10) {
    date = '0' + date;
  }
  return year + '-' + month + '-' + date;
}
