const formatTime = (date,type) => {
  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(type == 1){
	// 返回年月日
	return `${[year, month, day].map(formatNumber).join('-')}`
  }else{
	// 返回年月日时分秒
	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 computeDays = (createTimestamp,currentTimestmap)=>{
	let subTime = currentTimestmap - createTimestamp; // 毫秒级(注意传的参数是否为毫秒级)
	// 相差的天数
	let days = Math.floor(subTime / (24 * 3600 * 1000));
	return days;
}

// 倒计时
const daojishi = function(val1,mins){
	// 开始时间 + 过期时间 = 最后的时间
	// 最后的时间- 动态的当前时间 = 倒计时
	let that = this;
	let now = new Date(val1.replace(/-/g, "/"));  //处理开始时间
	let minutes = Number(mins);  // mins 分钟数
	let end = now.setMinutes(now.getMinutes() + minutes); // 计算最后时间  (调整过的日期的毫秒表示)
	let time_now =  new Date().getTime(); //当前时间
	let msec = end - time_now;   // 最后时间-当前时间
	if(msec > 0){
		let day = parseInt(msec / 1000 / 60 / 60 / 24)
		let hr = parseInt(msec / 1000 / 60 / 60 % 24)
		let min = parseInt(msec / 1000 / 60 % 60)
		let sec = parseInt(msec / 1000 % 60)
		day = day
		hr = hr > 9 ? hr : '0' + hr
		min = min > 9 ? min : '0' + min
		sec = sec > 9 ? sec : '0' + sec
		return  {timeStr: (min+'分'+ sec+'秒'),isend: false};
	} else {
		return {timeStr: '' ,isend: true};
	}
}
module.exports = {
  formatTime,
  computeDays,
  daojishi
}
