How could I get months week number based on year week number (i.e week 33)

。_饼干妹妹 提交于 2019-12-13 03:19:13

问题


I'm wondering how can I calculate what is the week of the month based on the ordinal number of the week of the year. For example I'm dealing with week 33, I should know that's Week 2 in august. I allready calculated months but now I'm dealing with weeks.

I allready have a solution, but it seems dirty to me..

Here's the code:

var data = query.GroupBy(x => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(x.CreatedDate ?? DateTime.UtcNow, CalendarWeekRule.FirstDay, DayOfWeek.Monday))
.Select(article => new ArticleSimpleObject
{
    Week = GetWeekNumberOfMonth(article.FirstOrDefault().CreatedDate.Value),
    Amount = article.Sum(x => x.Amount),
    Month = article.FirstOrDefault().CreatedDate.Value.Month
});

And here's the method which I used to get week numbers:

 private static int GetWeekNumberOfMonth(DateTime date)
 {
      date = date.Date;
      DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
      DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
      if (firstMonthMonday > date)
      {
          firstMonthDay = firstMonthDay.AddMonths(-1);
          firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
       }
      return (date - firstMonthMonday).Days / 7 + 1;
  }

As I wrote guys, this solutions works,

but I personally don't like it, I guess there is more elegant solution, and that's why I posted this question to help to myself and to future readers if some experienced person helps us to solve this :)

Maybe this could be solved based on Calendar class https://docs.microsoft.com/en-us/dotnet/api/system.globalization.calendar?view=netframework-4.8

I've tried some variant but I was not even close to solve it..

Thanks guys

Cheers


回答1:


One approach is to subtract the week of the year of the 1st day of the month of the date from the week of the year of the date. Like so:

void Main()
{
    Console.WriteLine(DateTime.Now.GetWeekOfMonth());       
}

public static class MyDateTimeExtensions
{
    private static GregorianCalendar _calendar = new GregorianCalendar();

    public static int GetWeekOfMonth(this DateTime date)
    {
        return 
            date.GetWeekOfYear()
            - new DateTime(date.Year, date.Month, 1).GetWeekOfYear()
            + 1;
    }

    private static int GetWeekOfYear(this DateTime date)
    {
        return _calendar.GetWeekOfYear(
            date, 
            CalendarWeekRule.FirstDay, 
            DayOfWeek.Sunday);
    }
}

This outputs 4 for the current date: Sept. 23rd, 2019.




回答2:


You can write a couple of general extensions to compute this a little bit simpler.

First, you need the first date of the week starting on a particular day of week for a date:

public static DateTime FirstDateOfWeekStarting(this DateTime aDate, DayOfWeek dow) => aDate.Date.AddDays((int)dow - (int)aDate.DayOfWeek);

Then, you can easily convert the day of month of that first date to the Week Number in the month:

public static int WeekStartingDOWNumOfMonth(this DateTime aDate, DayOfWeek dow) => (aDate.FirstDateOfWeekStarting(dow).Day-1) / 7 + 1;

And, for your specific case of weeks beginning with Monday,

public static int WeekStartingMonNumOfMonth(this DateTime aDate) => aDate.WeekStartingDOWNumOfMonth(DayOfWeek.Monday);


来源:https://stackoverflow.com/questions/58039103/how-could-i-get-months-week-number-based-on-year-week-number-i-e-week-33

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!