date difference with javascript -


im trying details of date difference between 2 dates in format

0 years 2 days 1 hours 20 minutes 47 second 

this code far ..which not working

<script>     var starting_date = '2017-08-02 08:32:28';     var closing_date= '2017-08-02 09:30:05';      window.onload=function() {       // month,day,year,hour,minute,second       uptime(starting_date); // ****** change line!     };     function uptime(countto) {       //now = new date();       countto = new date(countto);      //difference = (now-countto);        difference = (closing_date-countto);       days=math.floor(difference/(60*60*1000*24)*1);       years = math.floor(days / 365);       if (years > 1){ days = days - (years * 365)}       hours=math.floor((difference%(60*60*1000*24))/(60*60*1000)*1);       mins=math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);       secs=math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);       document.getelementbyid('years').firstchild.nodevalue = years;       document.getelementbyid('days').firstchild.nodevalue = days;       document.getelementbyid('hours').firstchild.nodevalue = hours;       document.getelementbyid('minutes').firstchild.nodevalue = mins;       document.getelementbyid('seconds').firstchild.nodevalue = secs;        cleartimeout(uptime.to);       uptime.to=settimeout(function(){ uptime(countto); },1000);     }      </script>      <div id="countup">       it's been       <span id="years">00</span>        <span class="timerefyears">years</span>       <span id="days">00</span>       <span class="timerefdays">days</span>       <span id="hours">00</span>       <span class="timerefhours">hours</span>       <span id="minutes">00</span>       <span class="timerefminutes">minutes</span>       <span id="seconds">00</span>       <span class="timerefseconds">second</span>     </div> 

current output

it's been nan years nan days nan hours nan minutes nan second 

if use current date working fine

now = new date(); difference = (now-countto);  

try replacing

var starting_date = '2017-08-02 08:32:28'; var closing_date= '2017-08-02 09:30:05'; 

with this?

var starting_date = new date('2017-08-02 08:32:28'); var closing_date=  new date('2017-08-02 09:30:05'); 

but recommend put timezone discussed in here

but if want lessen headaches on manipulating dates try use moment.js


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -