Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

726 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