JS countdown, distance from a date how much time

Source: Internet
Author: User

How much time does JS calculate from now to a moment, showing the current date time distance x years x months x days and X days x hours x minutes x seconds, if the given time is earlier than the current time, it is displayed as the distance from 2012-9-30 has been 1 days 22 hours 26 minutes 30 seconds format, If the given time is later than the current time, it is displayed as a 1-day, 1-hour, 32-minute, 20-second format from 2012-10-3 .
Examples of function invocation methods:
Count_down (' 2012-10-2 ');//Only give date
Count_down (' 2012-10-2 3:22 ');//give date and time
Count_down (' 2012-10-2-3:22:33 '); given date and time and minute seconds
Caveats: do not delete <span id= "show" >Loading...</span>
The date must begin with 4 digits and be written in strict accordance with the above example
Shape like 2012-30-50 25:88:99, month and day, and every minute and minute one is wrong, the program will not work properly.

<spanID= "Show">Loading ...</span><Scripttype= "Text/javascript">functionCount_down (o) {varDatatime=/^[\d]{4}-[\d]{1,2}-[\d]{1,2} ([\d]{1,2}:[\d]{1,2} (: [\d]{1,2})?)? $/Ig,str="', Conn,s; if(!O.match (datatime)) {Alert ('the parameter format is 2020-01-01[01:01[:01]].\r the contents within [] can be omitted'); return false; }        varsec=(NewDate (O.replace (/-/IG,'/') . GetTime ()- NewDate (). GetTime ())/;        if(sec> 0) {Conn='also have'; }Else{conn='is over'; SEC*=-1; } s={'days': Sec/ -/3600,'hours': Sec/3600%24, ' min ': Sec/ -% -,'seconds': Sec% -};  for(iinchs) {                if(Math.floor (s[i])>0) Str+=Math.floor (s[i])+i; }        if(Math.floor (sec)==0) {str='0 seconds'; } document.getElementById ('Show'). InnerHTML= 'Distance <u>' +o+ '</u>' +Conn+'<u>' +Str+ '</u>'; SetTimeout (function() {Count_down (o)}, +);} Count_down ('2012-1-1 00:00:00');</Script>

JS countdown, distance from a date how much time

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.