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?

on
Medium Priority
942 Views
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);
gsecs = Math.floor(ddiff.valueOf()/1000);
}
``````
Comment
Watch Question

## View Solution Only

Commented:
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);
gsecs = Math.floor(ddiff.valueOf()/1000);
}
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

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

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
##### Thanks for using Experts Exchange.

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