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