Count number of alternate Monday between two Dates in ASP.Net using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

Hello Sir,

I want to calculate the count of alternate mondays in the given range in asp.net.

Thanks.

You are viewing reply posted by: pandeyism 5 months ago.
Posted 5 months ago Modified on 5 months ago

Hey democloud,

Please refer below sample and refer below article.

AJAX Calendar Extender control with Time in ASP.Net

HTML

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function () {
        $(".test").dynDateTime({
            showsTime: true,
            ifFormat: "%m/%d/%y",
            daFormat: "%l;%M %p, %e %m, %Y",
            align: "BR",
            electric: false,
            singleClick: false,
            displayArea: ".siblings('.dtcDisplayArea')",
            button: ".next()"
        });
    });
</script>
<div>
    <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" class="test"></asp:TextBox>
    <img src="calender.png" alt="blank" />
    <asp:TextBox ID="TextBox2" runat="server" ReadOnly="true" class="test"></asp:TextBox>
    <img src="calender.png" alt="blank" />
    <asp:Button ID="btnSave" runat="server" Text="Count" OnClick="btnSave_Click" />
    <asp:GridView ID="gvDayes" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Days" HeaderText="Days" />
            <asp:BoundField DataField="Date" HeaderText="Date" />
        </Columns>
    </asp:GridView>
    <asp:Label ID="lblCount" runat="server" />
</div>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void btnSave_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Days");
    dt.Columns.Add("Date");
    DateTime startDate = DateTime.Parse(Request.Form[TextBox1.UniqueID]); ;
    DateTime endDate = DateTime.Parse(Request.Form[TextBox2.UniqueID]);
    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    {
        if (date.DayOfWeek == DayOfWeek.Monday)
        {
            dt.Rows.Add(date.DayOfWeek, date);
        }
    }

    IEnumerable<DataRow> result = (from products in dt.AsEnumerable()
                                    select products).Where((c, i) => i % 2 == 0);
    DataTable dt1 = dt.Clone();
    foreach (DataRow row in result)
    {
        dt1.ImportRow(row);
    }
    lblCount.Text = "Total number of monday in month : " + result.Count().ToString();
    gvDayes.DataSource = dt1;
    gvDayes.DataBind();
}

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As DataTable = New DataTable()
    dt.Columns.Add("Days")
    dt.Columns.Add("Date")
    Dim startDate As DateTime = DateTime.Parse(Request.Form(TextBox1.UniqueID))
    Dim endDate As DateTime = DateTime.Parse(Request.Form(TextBox2.UniqueID))
    Dim date1 As DateTime = startDate
    While date1 <= endDate
        If date1.DayOfWeek = DayOfWeek.Monday Then
            dt.Rows.Add(date1.DayOfWeek, date1)
        End If

        date1 = date1.AddDays(1)
    End While
    Dim result As IEnumerable(Of DataRow) = (From products In dt.AsEnumerable() Select products).Where(Function(c, i) i Mod 2 = 0)
    Dim dt1 As DataTable = dt.Clone()
    For Each row As DataRow In result
        dt1.ImportRow(row)
    Next

    lblCount.Text = "Total number of monday in month : " & result.Count().ToString()
    gvDayes.DataSource = dt1
    gvDayes.DataBind()
End Sub

Screenshot