Solved

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

Posted on 2004-10-01
5
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fix Form size HTML 16 31
Rotate images for Web Page 11 28
Duplicate Elements with JS / jQuery 3 24
How do I split a variable to newline 2 19
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

733 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