Nodejs/javaScrip 获取时间差函数,包括年,月,日,时,秒
**时间差计算函数(可以直接计算出秒、分钟、小时、天的差)**
//获取系统当前时间并转化成固定格式字符串"2018-09-09 00:00:00"
var Datetemp1=new Date();
var timesNow='';
timesNow=Datetemp1.getFullYear() + "-" + (Datetemp1.getMonth() + 1) + "-" + Datetemp1.getDate() + " " + Datetemp1.getHours() + ":" + Datetemp1.getMinutes() + ":" + Datetemp1.getSeconds()
//时间差计算函数(可以直接计算出秒、分钟、小时、天的差)
function GetDateDiff(startTime, endTime, diffType) {
//将xxxx-xx-xx的时间格式,转换为 xxxx的格式
startTime = startTime.replace(/\-/g, "/");
endTime = endTime.replace(/\-/g, "/");
//将计算间隔类性字符转换为小写
diffType = diffType.toLowerCase();
var sTime = new Date(startTime); //开始时间
var eTime = new Date(endTime); //结束时间
//作为除数的数字
var divNum = 1;
switch (diffType) {
case "misecond":
divNum = 1;
break;
case "second":
divNum = 1000;
break;
case "minute":
divNum = 1000 * 60;
break;
case "hour":
divNum = 1000 * 3600;
break;
case "day":
divNum = 1000 * 3600 * 24;
break;
default:
break;
}
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
//调用的话就直接把开始结束和要的时间差填入函数就行
var day = GetDateDiff(startTime, endTime, 'day');
获取两个时间月份差
//获取月份差函数,如果传进来data1和data2的是"2018-03-09 00:00:00"形式的字符串,
//也可以是其他的格式“"2018-03-09”
function GetMonthDiff(date1, date2){
date1 = new Date(date1);
date2 = new Date(date2);
var year1 = date1.getFullYear();
var month1 = date1.getMonth()+1;
var year2 = date2.getFullYear();
var month2 = date2.getMonth()+1;
if(year1 != year2){
if(month1<month2){
var res = (year2-year1)*12+(month2-month1);//月份差
return res
}else{
var res = (year2-year1)*12-(month1-month2);
return res
}
}else{
var res = month2-month1
return res
}
}
//测试
console.log(dealDate("2018-03-09 00:00:00","2018-09-09 00:00:00"));
//输出的结果是6
获取两个时间相差几年几月几日
//调用测试函数获得时间差结果为“6个月0天”
console.log(dealDate("2017-07-20","2018-01-28"));
//提示前面的日期小于后面的日期
function dealDate(date1,date2){
date1 = new Date(date1);//如果传进来的是"yyyy-MM-dd"形式的字符串
date2 = new Date(date2);
var year1 = date1.getFullYear();
var month1 = date1.getMonth()+1;
var day1 = date1.getDate();
var dateMax1 = new Date(year1,month1,0);
var dayMax1 = dateMax1.getDate();
var year2 = date2.getFullYear();
var month2 = date2.getMonth()+1;
var day2 = date2.getDate();
var dateMax2 = new Date(year2,month2,0);
var dayMax2 = dateMax2.getDate();
if(day1==dayMax1&&day2==dayMax2){
//开始日期和结束日期都是月底
//直接计算月份差,推算几年几个月
var res = (year2-year1)*12+(month2-month1);//月份差
var difYear = Math.floor(res/12);
var difMonth = res%12;
return ((difYear!=0)?(difYear+"年"):"")+difMonth+"个月";
}else if(day1==dayMax1&&day2<dayMax2){
//开始日期是月底,结束日期不是
//计算开始日期到结束日期上一个月的月底,再加上结束日期的天数
var month3 = month2-1;
var res = (year2-year1)*12+(month3-month1);//月份差
var difYear = Math.floor(res/12);
var difMonth = res%12;
var difDay = day2;
return ((difYear!=0)?(difYear+"年"):"")+((difMonth!=0)?(difMonth+"个月"):"")+difDay+"天";
}else if(day1<dayMax1&&day2<dayMax2){
//开始日期和结束日期都不是月底
//情况1,day1<day2
if(day1<=day2){
var res = (year2-year1)*12+(month2-month1);//月份差
var difYear = Math.floor(res/12);
var difMonth = res%12;
var difDay = day2-day1;
return ((difYear!=0)?(difYear+"年"):"")+((difMonth!=0)?(difMonth+"个月"):"")+difDay+"天";
}else{
var month3 = month2-1;
var res = (year2-year1)*12+(month3-month1);//月份差
var difYear = Math.floor(res/12);
var difMonth = res%12;
var difDayTemp = day1-day2;
var dateMax3 = new Date(year2,month3,0);
var dayMax3 = dateMax3.getDate();
var difDay = dayMax3-difDayTemp;
return ((difYear!=0)?(difYear+"年"):"")+((difMonth!=0)?(difMonth+"个月"):"")+difDay+"天";
}
}
}