javascript計算時間差は一日です.
2129 ワード
1、 , ,
var d1="2007-07-22 14";
var d2="2007-07-25 13";
var date1=null;
var date2=new Date();
function Swith(strdate){
var strYear=strdate.substring(0,4);
var strMonth=strdate.substring(5,7);
var strDay=strdate.substring(8,10);
var strHours=strdate.substring(11,13);
var strMinutes=strdate.substring(14,17);
return new Date(strYear,strMonth,strDay,strHours,strMinutes);
}
function GetTime(dateM,datetype){
var s; // 。
var MinMilli = 1000 * 60; // 。
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
s="";
if (datetype=="d"){
s +=Math.round(Math.abs(dateM/DyMilli))+" ";
}else if (datetype=="h"){
s +=Math.round(Math.abs(dateM/HrMilli))+" "
}else if (datetype=="m"){
s +=Math.round(Math.abs(dateM/MinMilli))+" ";
} else{
s +=Math.round(Math.abs(dateM/1000))+" "
}
return(s); // 。
}
alert(GetTime((Date.parse(Swith(d1))-Date.parse(Swith(d2))),"d"));
2、
var d1="2007-07-22 14";
var d2="2007-07-25 13";
function Swith(strdate){
var strYear=strdate.substring(0,4);
var strMonth=strdate.substring(5,7);
var strDay=strdate.substring(8,10);
var strHours=strdate.substring(11,13);
var strMinutes=strdate.substring(14,17);
return new Date(strYear,strMonth,strDay,strHours,strMinutes);
}
function GetTime0(dateM){
var MinMilli = 1000 * 60; // 。
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
//
var days=Math.floor(dateM/(DyMilli));
//
var leave1=dateM%(DyMilli); //
var hours=Math.floor(leave1/(HrMilli));
//
var leave2=leave1%(HrMilli); //
var minutes=Math.floor(leave2/(MinMilli));
//
var leave3=leave2%(MinMilli); //
var seconds=Math.round(leave3/1000);
return days+" "+hours+" ";
}
alert(GetTime0((Date.parse(Swith(d2))-Date.parse(Swith(d1)))));