JavaScript time difference calculation function code instance
<script language= "JavaScript" >
Date.prototype.dateDiff = function (interval,objdate) {
If the parameters are insufficient or the objdate is not a Date object, it is returned undefined
if (arguments.length<2| | Objdate.constructor!=date) return undefined;
Switch (interval) {
Calculate the second difference
Case "S": Return parseint ((objdate-this)/1000);
Calculation of Difference
Case "n": Return parseint ((objdate-this)/60000);
Calculate the time difference
Case "H": Return parseint ((objdate-this)/3600000);
Calculate the day difference
Case "D": Return parseint ((objdate-this)/86400000);
Calculate the week difference
Case "W": Return parseint ((objdate-this)/(86400000*7));
Calculate the month difference
Case "M": Return (Objdate.getmonth () +1) + ((Objdate.getfullyear ()-this.getfullyear ()) *12)-(This.getmonth () +1);
Calculate the Year Difference
Case "Y": Return Objdate.getfullyear ()-this.getfullyear ();
The input is misunderstood
Default:return undefined;
}
}
</script>
The example of calling this method is as follows:
<script language= "JavaScript" >
var SDT = new Date ("2004/05/20 07:30:00");
var EDT = new Date ("2005/05/20 08:32:02");
Document.writeln ("Seconds Difference:" +sdt.datediff ("s", EDT) + "<br>");
Document.writeln ("Spread:" +sdt.datediff ("n", EDT) + "<br>");
Document.writeln ("Time Difference:" +sdt.datediff ("H", EDT) + "<br>");
Document.writeln ("Day Difference:" +sdt.datediff ("D", EDT) + "<br>");
Document.writeln ("Week Difference:" +sdt.datediff ("W", EDT) + "<br>");
Document.writeln ("Month Difference:" +sdt.datediff ("M", EDT) + "<br>");
Document.writeln ("Year Difference:" +sdt.datediff ("Y", EDT) + "<br>");
</script>
<script>
var S1 = "2009/08/01";
var s2 = "2009/12/31";
S1 = new Date (S1);
S2 = new Date (s2);
var time= s2.gettime ()-s1.gettime ();
var days = parseint (Time/(1000 * 60 * 60 * 24));
Alert ("Days difference:" + day);
</script>
JavaScript calculates the time difference
<script>
var startdate= new Date ("2004/3/1 12:00");
var enddate= new Date ("2004/3/1 15:00");
var df= (Enddate.gettime ()-startdate.gettime ())/1000/3600;
Alert (df+ "hour");
</script>
<script language= "JavaScript" >
<!--
var st1 = "2009/8/25 19:35";
var st2 = "2009/8/25 19:28";
var time1 = new Date (ST1);
var tiem2 = new Date (ST2);
var days = (Time1.gettime ()-tiem2.gettime ())/1000/60;//min
alert (days);
Document.writeln (days);
-
</SCRIPT>
Ask for the time difference between two date subtraction with JavaScript!
var oldtime=date.parse ("2005-1-10 8:00:00");
var newtime=date.parse ("2005-1-11 12:00:00");
var diff = newtime-oldtime, get millisecond difference, convert to hours
Who can tell me how to convert the string: "2005-1-12 8:00:00" to a datetime type!
var str= "2005-1-12 8:00:00";
var arr=str.split ("");
var arr1=arr[0].split ("-");
var arr2=arr[1].split (":");
Note that the six parameters in the new date () are month and day, and the months start at 0,
var date=new date (arr1[0],arr1[1],arr1[2],arr2[0],arr2[1],arr2[2]);
Get current time
var now =new Date ();
var year = Now.getfullyear ();
var month = Now.getmonth () +1;
var day = Now.getdate ();
var hour = now.gethours ();
var minute = Now.getminutes ();
var second = Now.getseconds ();
JavaScript time difference calculation function code instance