export function getDate() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2)
}

export function getDatetime() {
  const now = new Date()
  const year = now.getFullYear()
  const month = now.getMonth() + 1
  const day = now.getDate()
  const hh = now.getHours()
  const mm = now.getMinutes()
  const ss = now.getSeconds()
  let clock = year + '-'
  if (month < 10) clock += '0'
  clock += month + '-'
  if (day < 10) clock += '0'
  clock += day + ' '
  if (hh < 10) clock += '0'
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return clock
}

export function formatDatetime(date) {
  if (typeof date === 'undefined') return null
  if (date === '' || date === null) return null
  if (typeof date === 'string') return date
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hh = date.getHours()
  const mm = date.getMinutes()
  const ss = date.getSeconds()
  let clock = year + '-'
  if (month < 10) clock += '0'
  clock += month + '-'
  if (day < 10) clock += '0'
  clock += day + ' '
  if (hh < 10) clock += '0'
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return clock
}

export function formatDate(date) {
  if (typeof date === 'undefined') return null
  if (date === '' || date === null) return null
  if (typeof date === 'string') return date
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2)
}

export function getOneMonthOffset() {
  const myDate = new Date()
  let year = myDate.getFullYear()
  let month = myDate.getMonth()
  let date = myDate.getDate()
  let end = year + '-' + ('0' + (month + 1)).slice(-2) + '-' + ('0' + date).slice(-2)
  if (month === 0) {
    year -= 1
    month = 12
  } else if (month === 2) {
    date = date > 28 ? 28 : date
  } else {
    date = date > 30 ? 30 : date
  }
  let start = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2)
  return { start, end }
}

export function getDateRangeFormatDate(data) {
  let startTime = ''
  let endTime = ''
  //用户手动输入了日期 并且没有按下大键盘上面的回车
  if (data === null || data.length === 0) return { startTime, endTime }
  if (typeof data[0].$d !== 'undefined') {
    startTime = formatDate(data[0].$d) + ' 00:00:00'
    endTime = formatDate(data[1].$d) + ' 23:59:59'
  } else if (data !== null) {
    startTime = formatDate(data[0]) + ' 00:00:00'
    endTime = formatDate(data[1]) + ' 23:59:59'
  }
  return { startTime, endTime }
}

export function getDateRangeFormatDateTime(data) {
  let startTime = ''
  let endTime = ''
  if (data === null) return { startTime, endTime }
  //用户手动输入了日期 并且没有按下大键盘上面的回车
  if (typeof data[0].$d !== 'undefined') {
    startTime = formatDatetime(data[0].$d)
    endTime = formatDatetime(data[1].$d)
  } else {
    startTime = formatDatetime(data[0])
    endTime = formatDatetime(data[1])
  }
  return { startTime, endTime }
}

export function formatMonth(date) {
  if (typeof date === 'undefined') return null
  if (date === '' || date === null) return null
  if (typeof date === 'string') return date
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  return year + '-' + ('0' + month).slice(-2)
}
