hello! i have 3 textfields: one for the arrival date, the 2nd is fordeparture date, last is for the number of nights. both dates are picked in a datepicker calendar popup. would it be possible to display the difference between the two dates automatically right after selecting a departure date? the dates are in d/m/yyyy format. the difference should appear in the number of nights textfield.
it would be easy if i use parse() then change the value to number of days... but parse() is for mm/dd/yyyy format.

arrival date: [______] [*datepicker img link for popup here*]
departure date: [______] [*datepicker img link for popup here*]

number of nights: [__]

hope to hear from you soon.
thanks!

One way is to subtract the UNPARSED values. They are in the form of the number of days elapsed since 1900. Any decimals in the numbers are portions of days.

You can always try to use the split function, extract the three components from the given date, make a new Date out of it. Use this procedure for both the dates.

Use: (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24) formula to get the number of days.

Say, the name of the textbox is "noofnightsTxtFld", how can I use new date()? where will I put the name of the textbox?
Thanks to the replies guys.

How about something like:

<html>
<head>
    <script>
    setDifference = function(form)
    {
        var x = document.getElementById('dtTxt1').value;
        var y = document.getElementById('dtTxt2').value;
        
        //assuming that the delimiter for dt time picker is a '/'.
        var arr1 = x.split('/');
        var arr2 = y.split('/');

        var dt1 = new Date();
        dt1.setFullYear(arr1[2], arr1[1], arr1[0]);
        var dt2 = new Date();
        dt2.setFullYear(arr2[2], arr2[1], arr2[0]);
        
        document.getElementById('resultTxt').value = (dt2.valueOf() - dt1.valueOf()) / (60 * 60 * 24 * 1000)
    }
    </script>
</head>
<body>
<form>
    <input type="text" name="dtTxt1" id="dtTxt1" />
    <br />
    <input type="text" name="dtTxt2" id="dtTxt2" />
    <br />
    <input type="text" name="resultTxt" id="resultTxt" />
    <br /><br />
    <input type="button" value="Submit" onclick="javascript:setDifference(this.form);" />
</form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.