Jump to content

Setting the date to 14 days previous!?


muncan

Recommended Posts

Hi,I am creating an elearning system for which the current date is to be displayed in the format DD/MM/YYYY. I also need to include a date 14 days previous to the current date in the same format.Displaying the current date in that format isnt a problem. I can change the date using the code:-<script type="text/javascript">var d = new Date()d.setDate (-14)document.write(d)</script>but this displays the date as 'Tue Apr 11 15:25:22 UTC+0100 2006' .Ive tried this code..<script type="text/javascript">var d=new Date()var day=d.getDate() - 14var month=d.getMonth() + 1var year=d.getFullYear()document.write(year + "/" + month + "/" + day)</script>..but this just deletes 14 off the current day number (which is no ruddy use!!)Please can someone help out...? :) ThanksMuncan

Link to comment
Share on other sites

You need first set new Date to date/time needed and then split that setted date to DD/MM/YYYY:

var myDate = new Date();myDate.setDate(myDate.getDate() - 14);document.write(myDate.getDate() + '/' + myDate.getMonth() + '/' + myDate.getFullYear());

Link to comment
Share on other sites

argh! sorry!

var myDate = new Date();myDate.setMonth(myDate.getMonth() + 1);myDate.setDate(myDate.getDate() - 14);alert(myDate.getDate() + '/' + myDate.getMonth() + '/' + myDate.getFullYear());

should work OK now? :)edit:-24 -> -14 :)

Edited by raimo
Link to comment
Share on other sites

var myDate = new Date();myDate.setMonth(myDate.getMonth() + 1);myDate.setDate(myDate.getDate() - 24);alert(myDate.getDate() + '/' + myDate.getMonth() + '/' + myDate.getFullYear());Superb,Seems so obvious now! :) Sh*t me up when '1/4/2006' alert msg popped up but bit of tweaking and all is well. Thanks again.Muncan.

Link to comment
Share on other sites

Sh*t me up when '1/4/2006' alert msg popped up but bit of tweaking and all is well. Thanks again.
LOL! sorry, that was second mistake for I, testing it and forget change date back to -14.But now it's OK. Reason of that +1 is: JS year start from zero. January is month 0.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...