We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

why cannot calculate 2 date time's difference?

Medium Priority
942 Views
Last Modified: 2013-12-17
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

Comment
Watch Question

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);
          }
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.