[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

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

0
techques
Asked:
techques
  • 2
1 Solution
 
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
 
madhevan_pillaiCommented:
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now