开发者

In .net, knowing the week number how can I get the weekdays date?

I want to create a function in C# which for a week number will return me the days, in that week.

For instance for week number 40, how can I get the days: 4/10, 5/10, 6/10, 7/10, 8/10, 9/10, 10/10.

Thank you in advance!开发者_如何学Python


I think this should do what you want:

    public static DateTime[] WeekDays(int Year, int WeekNumber)
    {
        DateTime start = new DateTime(Year, 1, 1).AddDays(7 * WeekNumber);
        start = start.AddDays(-((int)start.DayOfWeek));
        return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
    }

Although I treat Sunday as first day of week, if you want Monday as first day change range from (0,7) to (1,7).

If you want to conform the ISO standard, I think this should work:

    public static DateTime[] WeekDays(int Year, int WeekNumber)
    {
        DateTime start = new DateTime(Year, 1, 4);
        start = start.AddDays(-((int)start.DayOfWeek));
        start = start.AddDays(7 * (WeekNumber - 1));
        return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
    }


Note

I appear to have missed bug. The current code have been updated as of 2012-01-30 to account for this fact and we now derive the daysOffset based on Tuesday which according to Mikael Svenson appears to solve the problem. See this answer for details.

Most people tend to get this wrong and by wrong I mean not conform to the ISO8601 week date (we use this a lot in Sweden) standard. This calculation is kinda strange but it boils down to this code in .NET:

DateTime jan1 = new DateTime(yyyy, 1, 1);

int daysOffset = DayOfWeek.Tuesday - jan1.DayOfWeek;

DateTime firstMonday = jan1.AddDays(daysOffset);

var cal = CultureInfo.CurrentCulture.Calendar;

int firstWeek = cal.GetWeekOfYear(jan1, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

var weekNum = ww;

if (firstWeek <= 1)
{
    weekNum -= 1;
}

var result = firstMonday.AddDays(weekNum * 7 + d - 1);

return result;

It will compute the date of a year (yyyy), week number (ww) and day of week (d). It does so by first establishing the first of January using the built-in calendar (so this code can be customized). The reason this is a bit strange is because week 53 sometimes occur in January and sometimes week 1 occurs in December.

If you need go the otherway it's not entirely trivial but the correct way to do this in .NET is shown here.

var c = CultureInfo.CurrentCulture.Calendar;

// `FromDayOfWeek` fixes problem with the enumeration
// not based on Monday being the first day of the week
d = (byte)FromDayOfWeek(c.GetDayOfWeek(t));
switch (d)
{
    case 1:
    case 2:
    case 3:
        // see this for details
        // http://blogs.msdn.com/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx
        t = t.AddDays(3);
        break;
}

ww = (byte)c.GetWeekOfYear(t, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

// Adjust year when week 53 occurs in January or week 1 occurs in December 
if (ww == 53 && t.Month == 1)
{
    yyyy = (short)(t.Year - 1);
}
else if (ww == 1 && t.Month == 12)
{
    yyyy = (short)(t.Year + 1);
}
else
{
    yyyy = (short)t.Year;
}


The following function will help you to get starting date from week number and year

public DateTime GetFirstDayFromWeekNumber(int weekNumber, int weekYear)
        {
            DateTime beginingYear = new DateTime(weekYear, 1, 1);
            DateTime finalDate = new DateTime();
            int maxDayOfWeek = (int)DayOfWeek.Saturday;
            int yearStartDay = (int)beginingYear.DayOfWeek;
            int dayDeference = maxDayOfWeek - yearStartDay;

            if (weekNumber == 1)
            {
                finalDate = beginingYear.AddDays(-yearStartDay);
            }
            else if (weekNumber == 2)
            {
                finalDate = beginingYear.AddDays((dayDeference + 1));
            }
            else if (weekNumber > 2 && weekNumber <= 53)
            {
                finalDate = beginingYear.AddDays((dayDeference + 1) + ((weekNumber - 2)* 7));
            }
            return finalDate;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜