Results 1 to 3 of 3

Thread: Date Difference Computation

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default Date Difference Computation

    Folks,

    The code below appears to be not working. Might someone be able to point out the error of my ways?

    Code:
    <script>
    var now = new Date();
    var awoke = setFullYear(2019, 8, 18);					// September 18, 2019
    var difference = (now - awoke);						// raw time
    var daze = Math.floor (difference / (1000 * 60 * 60 * 24));	// convert to days
    document.write (daze);							// the envelope, please
    </script>
    Thank you!

    A.
    Last edited by marain; 09-19-2019 at 12:08 AM. Reason: To indicate resolved

  2. #2
    Join Date
    Mar 2011
    Posts
    1,900
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    Try running it in a browser and you get the error
    Uncaught ReferenceError: setFullYear is not defined
    Take a quick look at the definition of setFullYear (https://developer.mozilla.org/en-US/...te/setFullYear)
    Its a method on the date object.
    Code:
    var awoke = now.setFullYear(2019, 8, 18);
    So if you change that, the code runs, but won't work as intended, as it modifies the date object it is run on.

    If your intention is to get the difference between today and a specific date, try the following:
    Code:
    <script>
    	let oldDate = new Date("08/18/2019");
    	let currentDate = new Date();
    
    	let miliDifference = currentDate - oldDate;
    	let daysDifference = Math.floor(miliDifference / 86400000);
    	document.write(daysDifference);
    </script>

  3. The Following User Says Thank You to keyboard For This Useful Post:

    marain (09-18-2019)

  4. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default Your Suggestions Worked

    Thank you.

    I'll add an "official" thank you when I figure out how. (It's been a long time.)

    A.

Similar Threads

  1. passing & recieving a date with mysql using Jason's Date Input Calendar
    By kkslider in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 02-19-2012, 04:30 PM
  2. PHP Date Difference Function?
    By cancer10 in forum PHP
    Replies: 1
    Last Post: 05-25-2008, 12:29 PM
  3. Jason's Date Input Calendar onClick to change the showing date
    By williamstam in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-13-2008, 12:48 PM
  4. Replies: 1
    Last Post: 06-25-2007, 12:31 PM
  5. Difference Between Two Date
    By baltazhor200401 in forum JavaScript
    Replies: 1
    Last Post: 11-07-2006, 05:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •