CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    12

    Substring determines addition

    I'm not a Javascript guru and would appreciate some help. Basically, I'm taking the difference between two dates and then displaying the numbers of weeks with a remainder. If the first remainder decimal is 7 or greater, they want the number rounded up to the next whole number. If it's a whole number, just tack on a ".0" for consistency. I've tried the following, but it doesn't seem to work. Can anybody help or is there a cleaner way to do it?

    Code:
    var x = document.getElementById('txtDueDate').value;
    var y = document.getElementById('txtAdmit').value;
    
    var dt1 = Date.parse(x)
    var dt2 = Date.parse(y)
    
    
    var ga = Math.round(((280 - (dt1.valueOf() - dt2.valueOf()) / (60 * 60 * 24 * 1000)) / 7) * Math.pow(10, 1)) / Math.pow(10, 1);
    
    if (inStr(ga,".") = -1) {
        document.getElementById('txtGaAdmit').value=ga + ".0"
        }
    else {
        if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
            document.getElementById('txtGaAdmit').value = (ga.subStr(0, inStr(ga, ".") - 1) + 1) + ".0"   
        }
        else {
            document.getElementById('txtGaAdmit').value=ga
        }
    }
    Last edited by PeejAvery; May 28th, 2009 at 11:48 AM. Reason: Added code tags.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Smile Re: Substring determines addition

    Do you know the line where the program has a trouble?

    I guess there is a conversion to be done between strings of characters and numbers.
    Try replacing:
    Code:
    if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
    by:
    Code:
    if (Number(ga.subStr(inStr(ga,".")+1,1)) >= 7) {
    Instead of Number(), you could use parseInt() or parseFloat().

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: Substring determines addition

    Are you just trying to get how many possible weeks and days total between two dates? If so, here is a simple function which will do just that. If not, can you explain yourself more clearly, please?

    Code:
    <script type="text/javascript">
    function calculateWeeks(d1, d2) {
      d1 = Date.parse(d1);
      d2 = Date.parse(d2);
    
      if (d1 > d2) {
        alert("End date must come after start date.");
      }
      else {
        var days = (d2 - d1) / 1000 / 60 / 60 / 24;
        var weeks = Math.ceil(days / 7);
        var leftover = days &#37; 7;
    
        var output = weeks + " weeks";
        if (leftover > 0) {output += " and " + leftover + " days";}
    
        alert(output);
      }
    }
    
    calculateWeeks('5/28/2009', '6/23/2009');
    </script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured