Nodejs/javaScripは、年間、月、日、時、秒を含む時間差関数を取得します.
16165 ワード
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');
月の差を2つ取得します.// , 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
2つの時間の違いを獲得して、数年の何月何日ですか?// “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
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+" ";
}
}
}