在.NET中计算一周中的星期几

edd*_*ves 40 .net datetime

.NET库是否可以轻松返回给定日期的周数?例如,输入Year = 2010, Month = 1, Day = 25,应输出5周数.

我发现最接近的是Calendar.GetWeekOfYear,几乎就在那里.

Java有一个日期字符串格式"W",它每周返回一次,但我在.NET中看不到任何等价物.

jas*_*son 60

没有内置的方法来做到这一点,但这里有一个扩展方法,应该为你做的工作:

static class DateTimeExtensions {
    static GregorianCalendar _gc = new GregorianCalendar();
    public static int GetWeekOfMonth(this DateTime time) {
        DateTime first = new DateTime(time.Year, time.Month, 1);
        return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
    }

    static int GetWeekOfYear(this DateTime time) {
        return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

DateTime time = new DateTime(2010, 1, 25);
Console.WriteLine(time.GetWeekOfMonth());
Run Code Online (Sandbox Code Playgroud)

输出:

5
Run Code Online (Sandbox Code Playgroud)

您可以GetWeekOfYear根据自己的需要进行更改.


Svi*_*ish 21

没有直接的内置方法可以做到这一点,但它可以很容易地完成.这是一种扩展方法,可用于轻松获取日期的基于年份的周数:

public static int GetWeekNumber(this DateTime date)
{
    return GetWeekNumber(date, CultureInfo.CurrentCulture);
}

public static int GetWeekNumber(this DateTime date, CultureInfo culture)
{
    return culture.Calendar.GetWeekOfYear(date,
        culture.DateTimeFormat.CalendarWeekRule,
        culture.DateTimeFormat.FirstDayOfWeek);
}
Run Code Online (Sandbox Code Playgroud)

然后我们可以用它来计算基于月份的周数,就像Jason所显示的那样.一个文化友好的版本可能看起来像这样:

public static int GetWeekNumberOfMonth(this DateTime date)
{
    return GetWeekNumberOfMonth(date, CultureInfo.CurrentCulture);
}

public static int GetWeekNumberOfMonth(this DateTime date, CultureInfo culture)
{
    return date.GetWeekNumber(culture)
         - new DateTime(date.Year, date.Month, 1).GetWeekNumber(culture)
         + 1; // Or skip +1 if you want the first week to be 0.
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*ley 13

标题具有误导性.问题实际上是关于月份的日历周数(如果您计算日历上的行的周数),而不是实际的周数.

对于本月的周数,您可以使用:

VB:
    Function WeekNumber(TargetDate As Date) As Integer
        Return (TargetDate.Day - 1) \ 7 + 1
    End Function

C#:
    public int WeekNumber(DateTime TargetDate)
    {
        return (TargetDate.Day - 1) / 7 + 1;
    }
Run Code Online (Sandbox Code Playgroud)

  • 这与示例不符.2010年1月1日是星期五,所以第一个日历周只有两天.因此,使用这个公式通常在第4周以下的第25个实际上是在第5周.由于例子说2010年1月1日应该产生第5周,这个答案中的公式不能正确,因为它产生第4周(即(int((25-1)/ 7)+ 1)= 4. (3认同)
  • 我同意标题具有误导性。例如,我试图计算该月的第四个星期一。但是,当我使用此问题下提供的其他答案编写单元测试时,有些结果与我的预期不符。这是因为如果该月的第一天是星期二或一周的晚些时候,则结果会偏离一周。这个答案提供的代码是我最终用来解决问题的代码。 (2认同)

Gau*_*ssZ 5

如此处指定:http : //forums.asp.net/t/1268112.aspx

您可以使用:

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

public static int GetWeekOfMonth(DateTime dt)
{    

    int weekOfYear = Iso8601WeekNumber(dt);

    if (dt.Month == 1)
    {
        //week of year == week of month in January
        //this also fixes the overflowing last December week
        return weekOfYear;
    }

    int weekOfYearAtFirst = Iso8601WeekNumber(dt.AddDays(1 - dt.Day));
    return weekOfYear - weekOfYearAtFirst + 1;
}
Run Code Online (Sandbox Code Playgroud)

请注意,由于某些算法选择,您的需求可能会有所不同。即是一个星期,2月底的周一至周五也是3月的第一周,或者用什么标准定义一个月的“第一”周?ISO 8601:2004没有给出“每月的周数”的定义。