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

//计算相差天数
const datedifference = (sDate1, sDate2) => { //sDate1和sDate2是2006-12-18格式 
	// console.log("datedifference=",sDate1,sDate2)
	var dateSpan,
		tempDate,
		iDays;
	sDate1 = Date.parse(sDate1.replace(/-/g, '/'));
	sDate2 = Date.parse(sDate2.replace(/-/g, '/'));
	dateSpan = sDate2 - sDate1;
	dateSpan = Math.abs(dateSpan);
	iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
	return iDays
};

const getDays =(date1, date2)=> {
    const dateTime = 1000 * 60 * 60 * 24; //每一天的毫秒数
    const minusDays = Math.floor(((date2.getTime() - date1.getTime()) / dateTime));//计算出两个日期的天数差
    return Math.abs(minusDays);
}

/**
 * [dateAddDays 从某个日期增加n天后的日期]
 * @param  {[string]} dateStr  [日期字符串]
 * @param  {[int]} dayCount [增加的天数]
 * @return {[string]}[增加n天后的日期字符串]
 */
const dateAddDays=(dateStr,dayCount) => {
    var tempDate=new Date(dateStr.replace(/-/g,"/"));//把日期字符串转换成日期格式
    var resultDate=new Date((tempDate/1000+(86400*dayCount))*1000);//增加n天后的日期
    var resultDateStr=resultDate.getFullYear()+"-"+(resultDate.getMonth()+1)+"-"+(resultDate.getDate());//将日期转化为字符串格式
    return resultDateStr;
}

/**
 * 根据newDate 来获取加的日期
 */
const addDaysFromNewDate=(tempDate,dayCount) => {
    var resultDate=new Date((tempDate/1000+(86400*dayCount))*1000);//增加n天后的日期
    var resultDateStr=resultDate.getFullYear()+"/"+(resultDate.getMonth()+1)+"/"+(resultDate.getDate());//将日期转化为字符串格式
    return resultDateStr;
}

const getWeightDate = date => {
	var timearr = date.replace(" ", ":").replace(/\:/g, "-").split("-");
	var timestr = "" + timearr[1] + "/" + timearr[2]
	return timestr 
}

export default {
	formatTime,
	formatDate,
	datedifference,
	getDays,
	dateAddDays,
	addDaysFromNewDate,
	getWeightDate
}
