Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

746 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

8 Experts available now in Live!

Get 1:1 Help Now