如何从 C# 中的年和周数中获取月份数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/659183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:15:11  来源:igfitidea点击:

How do I get the month number from the year and week number in c#?

c#datetime

提问by Loris

As the title says, given the year and the week number, how do I get the month number?

正如标题所说,给定年份和周数,我如何获得月份数?

edit: if a week crosses two months, I want the month the first day of the week is in.

编辑:如果一周跨越两个月,我想要一周的第一天所在的月份。

edit(2): This is how I get the week number:

编辑(2):这就是我获得周数的方式:

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

I'm just trying to do the reverse.

我只是想反过来​​做。

采纳答案by Loris

This is what I ended up doing:

这就是我最终做的:

static int GetMonth(int Year, int Week)
{
    DateTime tDt = new DateTime(Year, 1, 1);

    tDt.AddDays((Week - 1) * 7);

    for (int i = 0; i <= 365; ++i)
    {
        int tWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            tDt, 
            CalendarWeekRule.FirstDay, 
            DayOfWeek.Monday);
        if (tWeek == Week)
            return tDt.Month;

        tDt = tDt.AddDays(1);
    }
    return 0;
}

I would have preferred something simpler, but it works :)

我更喜欢更简单的东西,但它有效:)

回答by Ed Guiness

Wouldn't it also depend on the day of the week?

不是也取决于星期几吗?

回答by leppie

You cant. You need at least the day on which the 1st week starts (or when the week starts), to get an accurate answer.

你不能。您至少需要第 1 周开始的那一天(或一周开始的那一天)才能获得准确的答案。

回答by Chathuranga Chandrasekara

You cant. A week may start in one month and end in another.

你不能。一周可能从一个月开始,在另一个月结束。

回答by Jim H.

I think you're assuming that a "week" is any group of 7 sequential days. It isn't. Given Year(2008), Week(5), you could be in either January or Febuary, depending on when your "week" starts.

我认为您假设“一周”是任何连续 7 天的组。不是。给定 Year(2008), Week(5),您可能在 1 月或 2 月,具体取决于您的“一周”何时开始。

回答by Perchik

Another problem you could face is that most years do not start at the beginning of a week, which shifts everything.

您可能面临的另一个问题是,大多数年份不是从一周开始的,这会改变一切。

回答by John Rasch

If you assume that the first day of your definition of week is the same day as the 1st day of the year, then this will work:

如果您假设周定义的第一天与一年中的第一天是同一天,那么这将起作用:

int year = 2000;
int week = 9;
int month = new DateTime(year, 1, 1).AddDays(7 * (week - 1)).Month;

Obviously, a trueanswer would depend on how you define the first day of the week, and how you define how a week falls into a month when it overlaps more than one.

显然,真正的答案取决于您如何定义一周的第一天,以及如何定义当一周有多个重叠时将其划分为一个月。

回答by Janbert

this should be able to help

这应该能够帮助

 public int getMonth(int weekNum, int year)
 {
     DateTime Current = new DateTime(year, 1, 1);
     System.DayOfWeek StartDOW = Current.DayOfWeek;
     int DayOfYear = (weekNum * 7) - 6; //1st day of the week

     if (StartDOW != System.DayOfWeek.Sunday) //means that last week of last year's month
     {
         Current = Current.AddDays(7 - (int)Current.DayOfWeek);
     }
     return Current.AddDays(DayOfYear).Month;
}

回答by ravula sandeep

// Calculate the week number according to ISO 8601

    public static int Iso8601WeekNumber(DateTime dt)
    {
            return  CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    }

// ...

DateTime dt = DateTime.Now;

// Calculate the WeekOfMonth according to ISO 8601
int weekOfMonth = Iso8601WeekNumber(dt) - Iso8601WeekNumber(dt.AddDays(1 - dt.Day)) + 1;

回答by Daniel Choi

Assumptions:

假设:

  1. Sunday is the first day of the week.
  2. Partial week still counts as week 1
  3. Outputs beginning and ending month as integer array.

    public int[] getMonth(int weekNum, int year)
    {
    
        DateTime StartYear = new DateTime(year, 1, 1);
        System.DayOfWeek StartDOW = StartYear.DayOfWeek;
        DateTime DayOfYearWeekStart = default(DateTime);
        DateTime DayOfYearWeekEnd = default(DateTime);
        int x = 0;
        if ((StartDOW == System.DayOfWeek.Sunday)) {
            DayOfYearWeekStart = StartYear.AddDays((weekNum - 1) * 7);
            DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
        } else {
            for (x = 0; x <= 7; x += 1) {
                if (StartYear.AddDays(x).DayOfWeek == DayOfWeek.Sunday) {
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
    
            if (weekNum == 1) {
                DayOfYearWeekStart = StartYear;
                DayOfYearWeekEnd = StartYear.AddDays(x - 1);
            } else if (weekNum > 1) {
                DayOfYearWeekStart = StartYear.AddDays(((weekNum - 2) * 7) + x);
                DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
            }
    
        }
    
        int[] Month = new int[2];
        Month[0] = DayOfYearWeekStart.Month;
        Month[1] = DayOfYearWeekEnd.Month;
    
        return Month;
    }
    
  1. 星期日是一周的第一天。
  2. 部分周仍算作第 1 周
  3. 将开始和结束月份输出为整数数组。

    public int[] getMonth(int weekNum, int year)
    {
    
        DateTime StartYear = new DateTime(year, 1, 1);
        System.DayOfWeek StartDOW = StartYear.DayOfWeek;
        DateTime DayOfYearWeekStart = default(DateTime);
        DateTime DayOfYearWeekEnd = default(DateTime);
        int x = 0;
        if ((StartDOW == System.DayOfWeek.Sunday)) {
            DayOfYearWeekStart = StartYear.AddDays((weekNum - 1) * 7);
            DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
        } else {
            for (x = 0; x <= 7; x += 1) {
                if (StartYear.AddDays(x).DayOfWeek == DayOfWeek.Sunday) {
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
    
            if (weekNum == 1) {
                DayOfYearWeekStart = StartYear;
                DayOfYearWeekEnd = StartYear.AddDays(x - 1);
            } else if (weekNum > 1) {
                DayOfYearWeekStart = StartYear.AddDays(((weekNum - 2) * 7) + x);
                DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
            }
    
        }
    
        int[] Month = new int[2];
        Month[0] = DayOfYearWeekStart.Month;
        Month[1] = DayOfYearWeekEnd.Month;
    
        return Month;
    }