Solved

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

Posted on 2004-10-01
5
435 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now