Solved

Javascript date calculation - the date 7 days ago???

Posted on 2004-10-01
5
437 Views
Last Modified: 2006-11-17
this will be a breeze for most of you! but im a total dummy when it comes to scripting! I have the following script..

function initdt(mf) {
var t = new Date();
var e = new Date();
mf.SD.value = t.getDate();
mf.SM.value = t.getMonth() + 1;
mf.SY.value = t.getFullYear();
mf.ED.value = e.getDate();
mf.EM.value = e.getMonth() + 1;
mf.EY.value = e.getFullYear();
}

i want "t" to be the date 7 days ago and "e" to be the date yesterday. Its to populate a couple of date dropdowns that I have set up. Can sombody add in the needed code?

Cheers people!
0
Comment
Question by:goldsmithwilliams
  • 2
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 250 total points
ID: 12199420
var t = new Date();
t.setDate( t.getDate()-7 );

var e = new Date();
e.setDate( e.getDate()-1 );
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12199429
0
 

Author Comment

by:goldsmithwilliams
ID: 12199703
It works to an extent, my dropdowns for the two dates now show the correct day and years but the months are now blank? it work beforehand, but they both defaulted to todays date?? any ideas? cheers  knightEknight!!

<script language="JavaScript">
function initdt(mf) {
var t = new Date();
t.setDate( t.getDate()-7 );
var e = new Date();
e.setDate( e.getDate()-1 );
mf.SD.value = t.getDate();
mf.SM.value = t.getMonth() + 1;
mf.SY.value = t.getFullYear();
mf.ED.value = e.getDate();
mf.EM.value = e.getMonth() + 1;
mf.EY.value = e.getFullYear();
}
</script>
0
 

Author Comment

by:goldsmithwilliams
ID: 12199741
sorry my mistake! i had 01,02,03,04 as the values for the months! it wont work unles i have 1,2,3,4 etc!!

it works now! cheers!!!
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12199751
dropdowns?  can you show the code for that?
You may need to do this:

mf.SM.value = (t.getMonth() + 1);
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question