tom smith

tom smith

  • NA
  • 17
  • 0

Calculate difference of two dates using dropdown (javascript) and manual date entry

Feb 25 2012 2:22 AM
Hi All,

I am into web application learning stage using VWD 2010, MS Access as the database and VB. I have two date fields on a form. After browsing through forums and the web, added javascript dropdown to the two textboxes. As long as I use the dropdown to choose dates, the no. of nights between these two dates, is calculated correctly, using a javascript function showDays(). But if i enter the dates manually in both these textboxes, i get an object required error.

Javascript function:

$(function(){
function showDays() {
var start = $('#<%= txtIn.ClientID %>').datepicker('getDate');
var end = $('#<%= txtOut.ClientID %>').datepicker('getDate');
if (!start || !end) return;
var days = (end - start) / 1000 / 60 / 60 / 24;
$('#<%= txtNights.ClientID %>').val(days);
}
$('#<%= txtIn.ClientID %>').datepicker({
dateFormat: 'dd-mm-yy',
onSelect: showDays
});
$('#<%= txtOut.ClientID %>').datepicker({
dateFormat: 'dd-mm-yy',
onSelect: showDays
});
});


Form.aspx:
 
<td class="AlBoxes" style="width: 40px">
<asp:TextBox ID="txtNights" runat="server"
onfocus="this.select(); showDays();" Width="30px" CssClass="AlBoxes"></asp:TextBox>
</td>
 
 
Since the no. of nights is calculated and displayed automatically upon choosing different dates (using the dropdown picker), i had like the no. of nights to be calculated on the txtNights' focus (if dates are keyed in manually)

I am probably referring to the function showDays wrongly or something and would appreciate some help in this regard.

Thanks,

Tom