//返回 例 2017
const formatYear = data => {
  var date = data.date;
  var types = data.types;
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if (types == 'year') {
    return [year].map(formatNumber).join('-')
  } else if (types == 'month') {
    return [month].map(formatNumber).join('-')
  } else if (types == 'day') {
    return [day].map(formatNumber).join('-')
  }

}

//返回 例 12月02日
const formatDate2 = date => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
}

//返回 例 2017-12-12
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-')
}
//返回 例 2017--12-12 12:30:00
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//返回 例 12-12 12:30:00
const formatTime2 = date => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()

  return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
//返回 小时12:00:00
const formatHour = data => {
  var date = data.date;
  var types = data.types;
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  if (types == 'hour') {
    return [hour].map(formatNumber)
  } else if (types == 'minute') {
    return [minute].map(formatNumber).join('-')
  } else if (types == 'second') {
    return [second].map(formatNumber)
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatYear: formatYear,
  formatDate2: formatDate2,
  formatHour: formatHour,
  formatTime2: formatTime2
}
