Retrieve all Dates between selected Bootstrap DateRangePicker Plugin using C# and VB.Net in ASP.Net

Last Reply 11 months ago By dharmendr

Posted 11 months ago

we are using bootstrap date range picker where we select the start date and end date.

In this selected date range we want to capture all the dates between start date to end date.

Posted 11 months ago

Hi Ajayk,

Check this example. Now please take its reference and correct your code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-daterangepicker/3.0.5/moment.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-daterangepicker/3.0.5/daterangepicker.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-daterangepicker/3.0.5/daterangepicker.css" />
    <script type="text/javascript">
        $(function () {
            $('input[id*="txtDateRange"]').daterangepicker({
                autoUpdateInput: true,
                opens: 'center',
                timePicker: false,
                showDropdowns: false,
                timePicker24Hour: true,
                timePickerIncrement: 15,
                minDate: moment(),
                startDate: moment().startOf('hour'),
                endDate: moment().startOf('hour').add(32, 'hour'),
                locale: {
                    separator: "-",
                    format: 'DD/MM/YYYY',
                    "firstDay": 1
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtDateRange" class="form-control" />
        <br />
        <asp:Button Text="Submit" runat="server" OnClick="Submit" class="btn btn-primary" />
        <br />
        <br />
        <asp:GridView runat="server" ID="gvDates" CssClass="table table-responsive" />
    </div>
    </form>
</body>
</html>

Code

C#

protected void Submit(object sender, EventArgs e)
{
    DateTime startDate = DateTime.ParseExact(txtDateRange.Text.Split('-')[0], "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    DateTime endDate = DateTime.ParseExact(txtDateRange.Text.Split('-')[1], "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    List<DateTime> dates = Enumerable.Range(0, (endDate - startDate).Days + 1).Select(day => startDate.AddDays(day)).ToList();
    gvDates.DataSource = dates;
    gvDates.DataBind();
}

VB.Net

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim startDate As DateTime = DateTime.ParseExact(txtDateRange.Text.Split("-"c)(0), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Dim endDate As DateTime = DateTime.ParseExact(txtDateRange.Text.Split("-"c)(1), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Dim dates As List(Of DateTime) = Enumerable.Range(0, (endDate - startDate).Days + 1).Select(Function(day) startDate.AddDays(day)).ToList()
    gvDates.DataSource = dates
    gvDates.DataBind()
End Sub

Screenshot