// 获取日期范围内所有日期
export function getMonthBetween(starDay, endDay) {
  var arr = [];
  var dates = [];
  // 设置两个日期UTC时间
  var db = new Date(starDay);
  var de = new Date(endDay);
  // 获取两个日期GTM时间
  var s = db.getTime() - 24 * 60 * 60 * 1000;
  var d = de.getTime() - 24 * 60 * 60 * 1000;
  // 获取到两个日期之间的每一天的毫秒数
  for (var i = s; i <= d; ) {
    i = i + 24 * 60 * 60 * 1000;
    arr.push(parseInt(i));
  }
  // 获取每一天的时间  YY-MM-DD
  for (var j in arr) {
    var time = new Date(arr[j]);
    var year = time.getFullYear(time);
    var mouth = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : "0" + (time.getMonth() + 1);
    var day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
    var YYMMDD = year + "-" + mouth + "-" + day;
    dates.push(YYMMDD);
  }
  return dates;
}

/**
 * 日期格式化
 */
export function dateFormat(date, format) {
  format = format || "yyyy-MM-dd hh:mm:ss";
  if (date !== "Invalid Date") {
    let o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      S: date.getMilliseconds(), //millisecond
    };
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return "";
}

//本年第一天
export function getYearFirstDay() {
  var date1 = new Date();
  var year1 = date1.getFullYear();
  var firstMonth = year1 + "-" + "01" + "-" + "01";

  return firstMonth;
}

//本年最后一天
export function getYearLastDay() {
  var date = new Date();
  var year1 = date.getFullYear();
  var lastDay = year1 + "-" + "12" + "-" + "31";
  return lastDay;
}

export function getDaysNoTime(num) {
  // 如果包括今天,如需获取当前日期的近7天,days应该为6
  let oneDay = 24 * 60 * 60 * 1000;
  let endTime = new Date(Date.now()); //当前时间为结束时间
  endTime = dateFormat(endTime, "yyyy-MM-dd");
  let startTime = new Date(Date.now() - num * oneDay);
  startTime = dateFormat(startTime, "yyyy-MM-dd");
  const days = {
    startTime,
    endTime,
  };
  return days;
}
