locked
calculate number of days from from and to date RRS feed

  • Question

  • User810354248 posted

    In my asp.net+sql+vb web is have a leave requisition page. In which i have from and to date field to select by date picker.

    if someone will enter FROM date and fill TO date the NUMBER OF DAYS should come automatically. 
    It is working in my laptop while testing. but when someone access it through LAN and enter the TO date then it gives error.

    my code is as under

    vb 

      Protected Sub lveto_TextChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles lveto.TextChanged
            Dim ObjLocDate As New Date
            Dim IntLocDays As Integer
            IntLocDays = Convert.ToInt32("1")
            ObjLocDate = lveto.Text
            ObjLocDate = ObjLocDate.AddDays(IntLocDays)
            onduty.Text = ObjLocDate.Date.ToString("dd-MM-yyyy")
            nodays.Text = Val(lveto.Text) - Val(lvefrom.Text) + 1
        End Sub

    aspx

    <asp:TextBox ID="lvefrom" runat="server"></asp:TextBox>

    <asp:TextBox ID="lveto" runat="server" AutoPostBack="True"></asp:TextBox>

    <asp:TextBox ID="nodays" runat="server" AutoPostBack="True" Width="35px" style="text-align: center"></asp:TextBox>

    </script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#<%= lvefrom.ClientID%>').datepicker({
                    dateFormat: 'dd-mm-yy',
                    changeMonth: true,
                    changeYear: true
                });
            });
    </script>
     
    <script type="text/javascript">
            $(document).ready(function () {
                $('#<%= lveto.ClientID%>').datepicker({
                    dateFormat: 'dd-mm-yy',
                    changeMonth: true,
                    changeYear: true
                });
            });
    </script>
    My laptop short date setting is : dd-MMM-yy
    Long date setiings : d MMMM, yyyy
    Tuesday, January 8, 2019 6:22 AM

Answers

  • User475983607 posted

    Cast the inputs as DateTime types and simply subtract one date from the other.

        Protected Sub lveto_TextChanged(sender As Object, e As EventArgs) Handles lveto.TextChanged
    
            Dim DateTo As DateTime
            If Not DateTime.TryParse(lveto.Text, DateTo) Then
                'Invalid date to
            End If
    
            Dim DateFrom As DateTime
            If Not DateTime.TryParse(lvefrom.Text, DateFrom) Then
                'Invalid date form
            End If
    
            Dim ts As TimeSpan = DateTo - DateFrom
            nodays.Text = ts.Days
    
    
        End Sub

    Please read the DateTime and TimeSpan reference documentation.  After 1000+ posts you should be familiar with basic types and the docs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 3:10 PM
  • User-943250815 posted

    nodays.text is 9 (it should be 10)

    9 is correct, if you need to consider Date2 - Date1 (inclusive), just add 1

    To get next day use AddDays
    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.adddays?view=netframework-4.7.2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 11:38 AM
  • User475983607 posted

    If i select 1 Jan 2019 in lvefrom.text and 10 Jan 2019 as lveto.text the out put in nodays.text is 9 (it should be 10) as per date calculation

    There's 9 days between the first and tenth. 

    Dates start a 12pm.  If you need to include the 10th (go to the 11th) then simply add one to the result.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 12:27 PM

All replies

  • User-943250815 posted

    What error?
    Please review your code it seems scrambled

    Tuesday, January 8, 2019 12:55 PM
  • User810354248 posted

    <asp:TextBox ID="lvefrom" runat="server"></asp:TextBox>

    <asp:TextBox ID="lveto" runat="server" AutoPostBack="True"></asp:TextBox>

    <asp:TextBox ID="nodays" runat="server" AutoPostBack="True" Width="35px" style="text-align: center"></asp:TextBox>

    datepicker code

    </script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#<%= lvefrom.ClientID%>').datepicker({
                    dateFormat: 'dd-mm-yy',
                    changeMonth: true,
                    changeYear: true
                });
            });
    </script>
    
    <script type="text/javascript">
            $(document).ready(function () {
                $('#<%= lveto.ClientID%>').datepicker({
                    dateFormat: 'dd-mm-yy',
                    changeMonth: true,
                    changeYear: true
                });
            });
    </script>

    Tuesday, January 8, 2019 2:29 PM
  • User475983607 posted

    Cast the inputs as DateTime types and simply subtract one date from the other.

        Protected Sub lveto_TextChanged(sender As Object, e As EventArgs) Handles lveto.TextChanged
    
            Dim DateTo As DateTime
            If Not DateTime.TryParse(lveto.Text, DateTo) Then
                'Invalid date to
            End If
    
            Dim DateFrom As DateTime
            If Not DateTime.TryParse(lvefrom.Text, DateFrom) Then
                'Invalid date form
            End If
    
            Dim ts As TimeSpan = DateTo - DateFrom
            nodays.Text = ts.Days
    
    
        End Sub

    Please read the DateTime and TimeSpan reference documentation.  After 1000+ posts you should be familiar with basic types and the docs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 3:10 PM
  • User810354248 posted

    Thanks for your reply

    tried the above code

    If i select 1 Jan 2019 in lvefrom.text and 10 Jan 2019 as lveto.text the out put in nodays.text is 9 (it should be 10) as per date calculation

    one more field i have which is onduty.text

    In which i want the next day of lveto.text (It means if lveto.text is 10 Jan 2019 then onduty.text shoul be 11 Jan 2019

    Wednesday, January 23, 2019 9:04 AM
  • User-943250815 posted

    nodays.text is 9 (it should be 10)

    9 is correct, if you need to consider Date2 - Date1 (inclusive), just add 1

    To get next day use AddDays
    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.adddays?view=netframework-4.7.2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 11:38 AM
  • User475983607 posted

    If i select 1 Jan 2019 in lvefrom.text and 10 Jan 2019 as lveto.text the out put in nodays.text is 9 (it should be 10) as per date calculation

    There's 9 days between the first and tenth. 

    Dates start a 12pm.  If you need to include the 10th (go to the 11th) then simply add one to the result.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 12:27 PM