Friday, June 8, 2012

Finding the last friday of a month

Here is the code

 //// First we set the num of day add to 1.
        int addDay = 1;

        //// Getting today's date.
        DateTime d = DateTime.Today;

        //// tis loop will execute until the month changes to next.
        while (d.Month == DateTime.Today.Month)
            DateTime friday;          

            //// Checks whether the date d is a friday or not.
            if (d.DayOfWeek == DayOfWeek.Friday)
                friday = d.Date;

                //// From here we add 7 days to the date taken.
                //// It is used to reduce the loop count.
                //// Adding 7 days will result to the next friday.
                addDay = 7;

            d = d.AddDays(addDay);
Finaly the friday will contain the last friday date of the month.
Similarly you can find out other week days. Cheers...

No comments:

Post a Comment