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(':')
}

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




const getCurrentMonthFirst = date  =>{
  date.setDate(1);
  let month = parseInt(date.getMonth()+1);
  let day = date.getDate();
  if (month < 10) {
      month = '0' + month
  }
  if (day < 10) {
      day = '0' + day
  }
  return date.getFullYear() + '-' + month + '-' + day;
}


const getCurrentMonthLast = function (date = null){
if(date==null) {
  date = new Date();
}
let currentMonth=date.getMonth();
let nextMonth=++currentMonth;
let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
let oneDay=1000*60*60*24;
let lastTime = new Date(nextMonthFirstDay-oneDay);
let month = parseInt(lastTime.getMonth()+1);
let day = lastTime.getDate();
  if (month < 10) {
      month = '0' + month
  }
  if (day < 10) {
      day = '0' + day
  }
  console.log(date.getFullYear() + '-' + month + '-' + day)
  return date.getFullYear() + '-' + month + '-' + day;
}

module.exports = {
  formatTime: formatTime,
  getCurrentMonthFirst:getCurrentMonthFirst,
  getCurrentMonthLast:getCurrentMonthLast
}