Add some time to a date script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TVining
    New Member
    • Dec 2007
    • 26

    Add some time to a date script

    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!

    Code:
    <script type="text/javascript">
    <!-- Hide this code from non-JavaScript browsers
    function GradDate() {
    UDate=document.forms[0].entry.value; // pick up user entry
    i1=UDate.indexOf('/'); // find first slash
    i2=UDate.indexOf('/',i1+1); // ...second slash
    UDate_M=UDate.substring(0,i1); // everything before first slash
    UDate_D=UDate.substring(i1+1,i2); // ...between two slashes
    UDate_Y=UDate.substring(i2+1); // ...after second slash
    UDate_Y=UDate_Y*1+2000; // 2-digit year to 4-digit
    UDate_M=UDate_M-1; // jan-dec=0-11
    Now=new Date(UDate_Y,UDate_M,UDate_D); // convert to a date
    Now_D=Now.getDay(); // sunday-saturday=0-6
    Diff=47-Now_D; // seventh Friday later
    Factor=24*60*60*1000; // seconds per day
    Target=new Date(Now*1+Diff*Factor); // the target date
    Target_M=Target.getMonth();
    Target_D=Target.getDate();
    Target_Y=Target.getYear();
    if (Target_Y < 70) { Target_Y=Target_Y+2000; }
    if (Target_Y < 1900) { Target_Y=Target_Y+1900; }
    Target_M=Target_M*1+1; // Jan-Dec = 1-12
    Formatted=Target_M+'-'+Target_D+'-'+Target_Y;
    document.forms[0].result.value=Formatted; // display on the form
    }
    // End hiding -->
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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).

    Comment

    • TVining
      New Member
      • Dec 2007
      • 26

      #3
      Thanks. Now for the big question....Whe re do I put that, and what would it look like?

      :-P

      Sorry, I'm script stupid.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Heh, two months later ;)

        Create a date and compare it with Now:
        [code=javascript]compareDate = new Date(2008,9,25) ;// Oct 25,2008
        //after declaring Now and Diff
        if (Now > compareDate) diff += 14;[/code]

        Comment

        Working...