muncan Posted April 25, 2006 Share Posted April 25, 2006 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 More sharing options...
raimo Posted April 25, 2006 Share Posted April 25, 2006 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 More sharing options...
muncan Posted April 25, 2006 Author Share Posted April 25, 2006 Thanks for that Raimo,Works a treat!Apart from its one month behind what it should be. Question now is, where do I put the '+ 1' to make it the correct month ?Cheers againMuncan. Link to comment Share on other sites More sharing options...
raimo Posted April 25, 2006 Share Posted April 25, 2006 (edited) 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 April 25, 2006 by raimo Link to comment Share on other sites More sharing options...
muncan Posted April 25, 2006 Author Share Posted April 25, 2006 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 More sharing options...
raimo Posted April 25, 2006 Share Posted April 25, 2006 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now