Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    JavaScript Dates, minus (w/formatting)/difference

    Hi so because the jquery range slider has no date range ability and because the only date range plugin for jquery UI sliders is very bloated and fails to work on modern ipad / new touch event methods... I'm writing my own today..

    Can some one better exmplain to me how to do:
    "theCurrentDate (05/12/2012) - anOlderDate (05/12/1990) = difference of how many days is?"

    and

    "theCurrentDate (05/12/2012) - 200 = but resulting in date formatted version, not some 9394829398 number"?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    <script>
    Dx = new Date("may 5,2012");
    Dy = new Date("may 5,1990");
    alert((Dx - Dy) / 86400000);
    alert(new Date(Dx-(200*86400000)))
    </script>
    <body>
    <script>
    document.write(new Date(Dx-(200*86400000)));
    </script>
    </body>
    Last edited by DaveyErwin; 03-15-2012 at 11:16 PM.

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Thank you so much DaveyErwin!

    One last question, is there a way to have the output of "new Date(Dx-(200*86400000))" be in this format, "18/10/2011" vs "Tue Oct 18 2011 00:00:00 GMT-0700 (Pacific Daylight Time)"?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Each part of the date is available as date methods

    getDate() returns the day of the month
    getMonth() returns one less than the month number
    getFullYear() returns the year

    so

    Code:
    dR = new Date(Dx-(200*86400000));
    alert(dR.getDate()+'/'+(dR.getMonth()+1)+'/'+dRGetFullYear());

    If you want to add all of the extra functionality for adding and subtracting dates and being able to easily select the final format using codes similar to the PHP format command then take a look at http://javascriptexample.net/dollarD.php
    Last edited by felgall; 03-16-2012 at 02:35 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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