Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

why cannot calculate 2 date time's difference?

Hi

I have the following js and it can show
            var dthen      =       2009-02-28 13:00:00
            var dnow      =       2009-02-18 13:00:00      

But, it shows NaN of the alert (ddiff) and (gsecs).

What's wrong with the code?

I use aspx, vc#.
StartCountDown("10","2009-02-28 13:00:00", "<%= SystemDateTime %>")
          function StartCountDown(myDiv,myTargetDate,sysDateTime)
          {
            var dthen	= new Date(myTargetDate); //2009-02-28 13:00:00
            var dnow	= new Date(sysDateTime);   //2009-02-18 13:00:00         
            ddiff		= new Date(dthen-dnow);
            window.alert(ddiff);
            gsecs = Math.floor(ddiff.valueOf()/1000);
            window.alert(gsecs);
          }

Open in new window

Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Hi,

Try this

            StartCountDown("10",'2009/02/28 13:00:00', "<%= System.DateTime.Now %>")
          function StartCountDown(myDiv,myTargetDate,sysDateTime)
          {
     
            var dthen      = new Date(myTargetDate);   //2009-02-28 13:00:00
            var dnow      = new Date(sysDateTime);     //2009-02-18 13:00:00        
            ddiff            = new Date(dthen-dnow);
            window.alert(ddiff);
            gsecs = Math.floor(ddiff.valueOf()/1000);
            window.alert(gsecs);
          }
ASKER CERTIFIED SOLUTION
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sybe
sybe

In javascript create a Date object with this:
dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])

the code you are using is actually this:
new Date('2009/02/28 13:00:00');

whioch does not seem to create a data object in javascript