在.net中,知道周数如何获得工作日日期?

发布于 2024-09-26 03:32:46 字数 112 浏览 8 评论 0 原文

我想在 C# 中创建一个函数,该函数将返回一周的天数。

例如,第 40 周,我如何获得天数: 4/10、5/10、6/10、7/10、8/10、9/10、10/10。

先感谢您!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-10-03 03:32:46

我认为这应该满足您的要求:

    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();
    }

虽然我将星期日视为一周的第一天,但​​如果您希望星期一作为第一天,则更改范围从 (0,7) 到 (1,7)。

如果你想符合ISO标准,我认为这应该可行:

    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();
    }

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();
    }
最近可好 2024-10-03 03:32:46

注意

我似乎错过了错误。当前代码已于 2012 年 1 月 30 日更新,以考虑到这一事实,我们现在根据 daysOffset users/153390/mikael-svenson">Mikael Svenson 似乎解决了这个问题。有关详细信息,请参阅此答案

大多数人往往会犯这个错误,我所说的错误是指不符合 ISO8601 周日期(我们在瑞典经常使用这个)标准。这个计算有点奇怪,但它可以归结为 .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;

它将计算一年中的日期 (yyyy)、周数 (ww) 和星期几 (d)。为此,它首先使用内置日历建立一月一日(因此可以自定义此代码)。这有点奇怪,因为第 53 周有时发生在 1 月,有时第 1 周发生在 12 月。

如果您需要采取其他方式,这并不完全是微不足道的,但此处显示了在 .NET 中执行此操作的正确方法。

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;
}

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;
}
寂寞花火° 2024-10-03 03:32:46

以下函数将帮助您从周数和年份获取开始日期

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;
        }

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文