Results 1 to 3 of 3

Thread: New to JavaScript and need some help

  1. #1
    Join Date
    Dec 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to JavaScript and need some help

    Hello all,

    I am new to the boards and to JavaScript and I am hoping some of you can help me. I am trying to design a script that will calcuate the number of days between two dates. I already have the basics below but it is the actual equation that I am having problem with. Any help would be appreciated.

    Here is the code I have so far.

    Code:
    <HEAD>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    
    function dateDiff(firstmonth, firstday, firstyear, secondmonth, secondday, secondyear) 
    {
     
        alert ("There are " + days + " days between the dates");
    }
    //  End -->
    </script>
    </HEAD>
    
    
    <BODY>
    
    <center>
    <form name=dateform>
    <table>
    <tr><td>
    <pre>
    First Date:   Month: <input type=text name=firstmonth value="" size=10 maxlength=10>  
                  Day: <input type=text name=firstday value="" size=10 maxlength=10>  
                  Year: <input type=text name=firstyear value="" size=10 maxlength=10>  
    
    Second Date:   Month: <input type=text name=secondmonth value="" size=10 maxlength=10>  
                  Day: <input type=text name=secondday value="" size=10 maxlength=10>  
                  Year: <input type=text name=secondyear value="" size=10 maxlength=10>  
    
    <center><input type=button value="Calculate Difference in days!" 
         onclick="dateDiff(firstmonth.value, firstday.value, firstyear.value, secondmonth.value,
    		       secondday.value, secondyear.value);">
    
    </center>
    </pre>
    </td></tr>
    </table>
    </form>
    </center>
    Thank you,

    LL

  2. #2
    Join Date
    Dec 2007
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I'm by no means a javascript guru but I wanted to throw this out there. I don't see in your function that you are doing any math. Maybe this snippet I found elsewhere might help you out.

    var strDate1 = document.formName.fieldName.value
    var strDate2 = document.formName.fieldName.value

    datDate1= Date.parse(strDate1);
    datDate2= Date.parse(strDate2);

    datediff = ((datDate1-datDate2)/(24*60*60*1000))

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's how it works. But, you haven't defined datDate1, datDate2, or datediff, and accessing form elements should be done as document.forms.formName.elements.fieldName to avoid possible conflicts.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •