// 时间转换方法
function format(time, format) {
  // 需要补三位 数据库只存了十位
  var t = new Date(parseInt(time + '000'))
  var tf = function(i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear())
        break
      case 'MM':
        return tf(t.getMonth() + 1)
        break
      case 'mm':
        return tf(t.getMinutes())
        break
      case 'dd':
        return tf(t.getDate())
        break
      case 'HH':
        return tf(t.getHours())
        break
      case 'ss':
        return tf(t.getSeconds())
        break
    }
  })
}
function dateFormat(d) {
  const year = d.getFullYear()
  let month = d.getMonth() + 1 + ''
  if (month <= 9) {
    month = '0' + month
  }
  let date = d.getDate() + ''
  if (date <= 9) {
    date = '0' + date
  }
  let hour = d.getHours() + ''
  if (hour <= 9) {
    hour = '0' + hour
  }
  let minute = d.getMinutes() + ''
  if (minute <= 9) {
    minute = '0' + minute
  }
  let second = d.getSeconds() + ''
  if (second <= 9) {
    second = '0' + second
  }
  return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
  // return year + "/" + month + "/" + date + " " + hour + ":" + minute + ":" + second;
}
// dateFormat(new Date());
// 将秒转换为时分秒，
function second(endTime) {
  let secondTime = parseInt(endTime)// 将传入的秒的值转化为Number
  let min = 0// 初始化分
  let h = 0// 初始化小时
  let result = ''
  if (secondTime > 60) { // 如果秒数大于60，将秒数转换成整数
    min = parseInt(secondTime / 60)// 获取分钟，除以60取整数，得到整数分钟
    secondTime = parseInt(secondTime % 60)// 获取秒数，秒数取佘，得到整数秒数
    if (min > 60) { // 如果分钟大于60，将分钟转换成小时
      h = parseInt(min / 60)// 获取小时，获取分钟除以60，得到整数小时
      min = parseInt(min % 60) // 获取小时后取佘的分，获取分钟除以60取佘的分
    }
  }
  result = `${h.toString().padStart(2, '0')}:${min.toString().padStart(2, '0')}:${secondTime.toString().padStart(2, '0')}`
  return result
}
// 获取本周的最后一天
function getLastWeekDay(date = new Date()) {
  const weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
  const weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
  let lastMonth = Number(weekLastDay.getMonth()) + 1
  if (lastMonth < 10) {
    lastMonth = '0' + lastMonth
  }
  let weekLastDays = weekLastDay.getDate()
  if (weekLastDays < 10) {
    weekLastDays = '0' + weekLastDays
  }
  return weekFirstDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays
}
// 获取本周第一天
function getFiLstWeekDay(date = new Date()) {
  const weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
  let firstMonth = Number(weekFirstDay.getMonth()) + 1

  if (firstMonth < 10) {
    firstMonth = '0' + firstMonth
  }
  let weekFirstDays = weekFirstDay.getDate()
  if (weekFirstDays < 10) {
    weekFirstDays = '0' + weekFirstDays
  }
  return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays
}
// 获取当月第一天天数
function getFilstMonthDays(time = new Date()) {
  var date = new Date(time)
  date.setDate(1)
  var month = parseInt(date.getMonth() + 1)
  var day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return date.getFullYear() + '-' + month + '-' + day
}
// 获取当月最后一天天数
function getLastMonthDays(time = new Date()) {
  var endDate = new Date(time)
  var currentMonth = endDate.getMonth()
  var nextMonth = ++currentMonth
  var nextMonthFirstDay = new Date(endDate.getFullYear(), nextMonth, 1)
  var oneDay = 1000 * 60 * 60 * 24
  var lastTime = new Date(nextMonthFirstDay - oneDay)
  var endMonth = parseInt(lastTime.getMonth() + 1)
  var endDay = lastTime.getDate()
  if (endMonth < 10) {
    endMonth = '0' + endMonth
  }
  if (endDay < 10) {
    endDay = '0' + endDay
  }

  return endDate.getFullYear() + '-' + endMonth + '-' + endDay
}
module.exports = {
  formatTime: format,
  second: second,
  dateFormat: dateFormat,
  getLastWeekDay: getLastWeekDay,
  getFiLstWeekDay: getFiLstWeekDay,
  getFilstMonthDays: getFilstMonthDays,
  getLastMonthDays: getLastMonthDays
}
