JS implementation calculates a time difference of two times

Source: Internet
Author: User

The JS implementation calculates a time difference of two times:
In practice, it is necessary to calculate the gap between the two time points, which is generally the difference between the current time and a specified point in time, and sometimes it needs to be accurate to days, hours, minutes, and seconds, and here's a quick introduction to how to achieve this effect.
The code is as follows:

<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /> <title>Ant Tribe</title> <styletype= "Text/css">#thenceThen{font-size:2em;}  </style>  <Scripttype= "Text/javascript">  functionThencethen () {varThetime="2014/5/4"   varEndTime=NewDate (thetime); varTotalsecs=(EndTime-NewDate ())/1000;   var Days=Math.floor (Totalsecs/3600/ -); varhours=Math.floor ((Totalsecs- Days* -*3600)/3600);   varmins=Math.floor ((Totalsecs- Days* -*3600-hours*3600)/60);   varsecs=Math.floor ((Totalsecs- Days* -*3600-hours*3600-mins* -)); if( days!=0) {document.getElementById ("Thencethen"). InnerHTML= Days+"days"+hours+"hours"+mins+"minutes"+secs+"seconds"; }   Else if(Hours==0&&mins==0) {document.getElementById ("Thencethen"). InnerHTML=secs+"seconds"; }   Else if(Hours==0&&mins!= 0) {document.getElementById ("Thencethen"). InnerHTML=mins+"minutes"+secs+"seconds"; }   Else if(Hours!=0) {document.getElementById ("Thencethen"). InnerHTML=hours+"hours"+mins+"minutes"+secs+"seconds"; }  }  varclock; Window.onload=function() {clock=SetInterval ("Thencethen ()", -); }  </Script>  </Head>  <Body>  <DivID= "Thencethen"></Div>  </Body>  </HTML>

The above code implements the function we want, the following is a brief introduction to the implementation of this effect.
I. Principle of implementation:
the principle is very simple, is to calculate the millisecond gap between a point in time, and then after the mathematical calculation of the corresponding day, hour, minute and description, through the SetInterval () function per second call function, then is the first inverted effect.
two. Code comments:
1.function Thencethen () {}, this function is used to calculate the time gap.
2.var thetime= "2014/5/4", this variable is used to define a point in time when the difference is to be calculated.
3.var endtime=new Date (thetime), which creates the current time object.
4.var totalsecs= (endtime-new Date ())/1000, the difference between two time objects is the millisecond gap between them, and divided by 1000 is the description of the difference.
5.var Days=math.floor (totalsecs/3600/24), calculate the difference in number of days, pay particular attention to the role of Math.floor () function, you can see the relevant reading.
6.var Hours=math.floor ((totalsecs-days*24*3600)/3600), calculates the number of hours of difference.
three. Related reading:
the parameters of the 1.Date () constructor can be found in the description of the Date () constructor parameter in JavaScript .
2. The Math.floor () function can be found in the Math.floor () method section of JavaScript .
The 3.innerHTML attribute can be found in the usage section of the JS innerHTML property .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9543

For more information, refer to: http://www.softwhy.com/javascript/

JS implementation calculates a time difference of two times

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.