SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Date Issue

    I've posted MUCH cleaner code with just the two fields needed to solve my problem. If I type in a date of 04/16/2011 in the "sentence date" field, then click the calculate button, the date 03/05/2011 is displayed in the "Due to Reviewer" field. It subtracts 42 days from the date enetred into the "Sentence Date" field.

    My problem is, sometimes when I type in a date in the "Sentence Date", such as 04/16/2011, 42 days prior falls on a weekend & I need it to always be on a weekday. So with that said, is it possible to put some type on function in the code where if the date falls on a weekend (Saturday or Sunday), it will roll back & display Friday's date?

    Thanks again for the help!



    <html>
    <head>
    <script type="text/javascript">
    var valid;

    function d2(v) { return (v<10)?("0"+v):v; }

    function dcheck(form) {

    var s = form.sent_date.value;
    var sent = new Date(s);
    var dr = form.due_rev.value
    var due_rev = new Date(dr);

    if (isNaN(due_rev)) {
    due_rev = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-42);
    }

    form.sent_date.value = (sent.getFullYear()+0) + "-" + d2(sent.getMonth()+1) + "-" + d2(sent.getDate());
    form.due_rev.value = (due_rev.getFullYear()+0) + "-" + d2(due_rev.getMonth()+1) + "-" + d2(due_rev.getDate());

    return true;
    }

    </script>

    </head>
    <body>
    <form method="post" action="add.php">

    </select>
    </br>

    <b>Sentence Date: MM/DD/YYYY</b> <br />
    <input type="text" name="sent_date" size="30" /><br />

    <p><input type="button" value="Calculate" onclick="return dcheck(this.form);"> </p>

    <b>Due to Reviewer/b> <br />
    <input type="text" name="due_rev" size="30" /><br />

    <br>

    </form>
    </body>
    </html>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    See Add or Subtract Business Days for a business days method that you can add to all your dates.

    Then you can just use:

    due_rev = new Date();
    due_rev.addBusDays(-30);

    (subtracting 30 business days should work out the same as subtracting 42 days except when the result would be on a weekend)
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still can't get it to work. Can someone insert the code in my code so I can test it? I have absolutley no experience in programming. I desparetley need assistance. This seems like an easy fix but I just don't understand it. Thanks for any help given!

  4. #4
    SitePoint Addict AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a way to do it by subtracting days until you get to the Friday before the weekend. I have added a few extra boxes so that you can see what is going on.

    Code :
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     
    <head>
    <title>Date adjustment</title>
    <script type="text/javascript">
    <!--
    var gap=42, days=["Sun","Mon","Tues","Wed","Thurs","Fri","Sat"];
    //
     function dcheck(form) 
       { var sent = new Date(form.sent_date.value);
         form.input_day.value =days[sent.getDay()];
         var i, prevDate;
       // take off days here 
         prevDate=new Date(sent.setDate(sent.getDate()-gap+1));
       // check if weekend (day=0 or 6)  
         for(i=0;i<3;i++)
           { prevDate=new Date(prevDate.setDate(prevDate.getDate()-1));
             if(prevDate.getDay()>0 && prevDate.getDay()<6){ break;} 
           }
        //      
       // add result to form 
        form.due_rev.value = (prevDate.getFullYear()+0) + "-" + d2(prevDate.getMonth()+1) + "-" + d2(prevDate.getDate());
       // show day of week 
        form.due_day.value =days[prevDate.getDay()];
       // show days difference  
        var daysDiff=(new Date(form.sent_date.value).getTime()-new Date(form.due_rev.value).getTime());
        form.days_diff.value=Math.ceil(daysDiff/(60*60*24*1000));
       }
    // -------
     function d2(v) { return (v<10)?("0"+v):v; }
    /   
    //-->
    </script>
    <style type="text/css">
    <!--
    p { margin-top:0px; margin-bottom:3px; }
    -->
    </style>
    </head>
     
    <body>
     
    <form method="get" action="a110305_1.htm">
      <p><b>Sentence Date: MM/DD/YYYY</b></p>
      <p><input type="text" name="sent_date" size="30" value="03/27/2011"></p>
      <p><input type="text" name="input_day" size="30"></p>
      <p><input type="button" value="Calculate" onclick="dcheck(this.form)"></p>
      <p><input type="text" name="days_diff" size="30"></p>
      <p><b>Due to Reviewer</b></p>
      <p><input type="text" name="due_rev" size="30"></p>
      <p><input type="text" name="due_day" size="30"></p>
      <p><input type="reset" value="Reset form" name="R1"> </p>
    </form>
     
    </body>
     
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •