...

View Full Version : JavaScript Dates, minus (w/formatting)/difference



ubh
03-15-2012, 11:28 PM
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!

DaveyErwin
03-15-2012, 11:53 PM
<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>

ubh
03-16-2012, 12:39 AM
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)"?

felgall
03-16-2012, 03:29 AM
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


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum