Connecting Tech Pros Worldwide Forums | Help | Site Map

Add some time to a date script

Newbie
 
Join Date: Dec 2007
Posts: 26
#1: Mar 28 '08
I have the following script on my website. The idea is that the user puts in a date, say January 1st. The script has to figure what day they actually will graduate a course. The students enter Monday, Tues, Wed, or Thurs, and then graduate on the 7th Friday. (regardless of what day they enter)

However, I need to modify it so that if the "enter date" is beyond 25 oct 2008, they graduate on the 9th Friday.

Can anyone help? I appologize, I'm just not a script type.

Thank you!

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!-- Hide this code from non-JavaScript browsers
  3. function GradDate() {
  4. UDate=document.forms[0].entry.value; // pick up user entry
  5. i1=UDate.indexOf('/'); // find first slash
  6. i2=UDate.indexOf('/',i1+1); // ...second slash
  7. UDate_M=UDate.substring(0,i1); // everything before first slash
  8. UDate_D=UDate.substring(i1+1,i2); // ...between two slashes
  9. UDate_Y=UDate.substring(i2+1); // ...after second slash
  10. UDate_Y=UDate_Y*1+2000; // 2-digit year to 4-digit
  11. UDate_M=UDate_M-1; // jan-dec=0-11
  12. Now=new Date(UDate_Y,UDate_M,UDate_D); // convert to a date
  13. Now_D=Now.getDay(); // sunday-saturday=0-6
  14. Diff=47-Now_D; // seventh Friday later
  15. Factor=24*60*60*1000; // seconds per day
  16. Target=new Date(Now*1+Diff*Factor); // the target date
  17. Target_M=Target.getMonth();
  18. Target_D=Target.getDate();
  19. Target_Y=Target.getYear();
  20. if (Target_Y < 70) { Target_Y=Target_Y+2000; }
  21. if (Target_Y < 1900) { Target_Y=Target_Y+1900; }
  22. Target_M=Target_M*1+1; // Jan-Dec = 1-12
  23. Formatted=Target_M+'-'+Target_D+'-'+Target_Y;
  24. document.forms[0].result.value=Formatted; // display on the form
  25. }
  26. // End hiding -->
  27.  

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Apr 1 '08

re: Add some time to a date script


Create a date object using the Date() constructor for the 25th of October and compare to the inputted date.

If it's greater than, add 14 days to the 7th Friday (to get the 9th Friday).
Newbie
 
Join Date: Dec 2007
Posts: 26
#3: May 31 '08

re: Add some time to a date script


Thanks. Now for the big question....Where do I put that, and what would it look like?

:-P

Sorry, I'm script stupid.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: May 31 '08

re: Add some time to a date script


Heh, two months later ;)

Create a date and compare it with Now:
Expand|Select|Wrap|Line Numbers
  1. compareDate = new Date(2008,9,25);// Oct 25,2008
  2. //after declaring Now and Diff
  3. if (Now > compareDate) diff += 14;
Reply