How to Calculate Number of Sunday in a particular month by asp.net programming

How to Calculate Number of Sunday in a particular month by asp.net programming:



When we make Pay roll type system the and many other time where we want to get the number of Sunday (how many time a particular day come in a particular month);
For this we create a particular function in C# programming Language .i think it is use full for you.  




private int GetOccuranceOfWeekday(int Year, int Month, DayOfWeek Weekday)
    {
        int ReturnValue = 0;
        DateTime MyDate = new DateTime(Year, Month, 1);
        int Start = 1;
        if (Weekday != MyDate.DayOfWeek)
        {
            Start = -(MyDate.DayOfWeek - Weekday - 1);
            if (Start <= 0)
            {
                ReturnValue = -1;
            }

        }
        while (Start <= DateTime.DaysInMonth(Year, Month))
        {
            ReturnValue += 1;
            Start += 7;
        }
        return ReturnValue;
    }

The name of this function is ‘GetOccuranceOfWeekday’ as you see this function take 3 variables as a argument and return a particulate value.

How to this Function:
We can call this function as like this ……..

Label1.Text=(GetOccuranceOfWeekday(2013, 10, DayOfWeek.Sunday)).ToString();



Code of  .Aspx page :


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="numberodsundayin_month.aspx.cs" Inherits="numberodsundayin_month" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>



Other Asp.net Related Post: