How to get the week number for a date in C# - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Thursday, October 13, 2011

How to get the week number for a date in C#

Today I needed to know which week of the year a certain day was in. My first instinct was DateTime.Now.Weekbut it seems there is no Week property on a DateTime instance. So I started looking for a way to get the week number. As it turns out the CultureInfo class will get you where you want to go using the Calendar property.

public static class DateTimeExtensions
{
    public static int WeekNumber(this System.DateTime value)
    {
        return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(value, CalendarWeekRule.FirstFourDayWeek,
                                                                 DayOfWeek.Monday);
 
    }
}

No comments:

Post a Comment