how to get Asp.net the First Sunday of the Given Month

How to get the First Sunday of the Given Month in Asp.net:



 By programming code we can get the frist sunday of the give moth . and the add 7 in first Sunday we can get second .similarly third ,fourth etc .
here we give you a C# function that take month name and year as input and give first Sunday as out put :


FUNCTION IS :

this Function give (return a int value ); i think it help you in programming 

  public int GetStartSunday()
    {
        int a;
        DateTime dt = new DateTime(d.Year, d.Month, 1);
        string dayofweek = dt.DayOfWeek.ToString();
        if (dayofweek == "Sunday")
        {
            a = 1;
        }
        else if (dayofweek == "Monday")
        {
            a = 7;
        }
        else if (dayofweek == "Tuesday")
        {
            a = 6;
        }
        else if (dayofweek == "Wednesday")
        {
            a = 5;
        }
        else if (dayofweek == "Thursday")
        {
            a = 4;
        }
        else if (dayofweek == "Friday")
        {
            a = 3;
        }
        else
        {
            a = 2;
        }


        return a;
    }

 

FUNCTION FOR GET NUMBER OF SUNDAY IN MOTH:

we can also get the number of total sunday by logic in asp.net programming :

Here we give a function :

 static int CountSundays(int year, int month)
    {
        var firstDay = new DateTime(year, month, 1);
        var day29 = firstDay.AddDays(28);
        var day30 = firstDay.AddDays(29);
        var day31 = firstDay.AddDays(30);
        if ((day29.Month == month && day29.DayOfWeek == DayOfWeek.Sunday)
        || (day30.Month == month && day30.DayOfWeek == DayOfWeek.Sunday)
        || (day31.Month == month && day31.DayOfWeek == DayOfWeek.Sunday))
        {
            return 5;
        }
        else
        {
            return 4;
        }

Comments

Popular posts from this blog