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

techquesAsked:
Who is Participating?
 
madhevan_pillaiConnect With a Mentor Commented:
The difference is here

StartCountDown("10","2009-02-28 13:00:00", "<%= SystemDateTime %>") - old


StartCountDown("10",'2009/02/28 13:00:00', "<%= System.DateTime.Now %>") - new
0
 
madhevan_pillaiCommented:
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);
          }
0
 
sybeCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.