如何获得基于年周数的月周数(即第33周) [英] How could I get months week number based on year week number (i.e week 33)
问题描述
我想知道如何根据一年中星期几的序数来计算一周中的星期几.例如,我要处理第33周,我应该知道8月是第2周. 我已经计算了数月,但现在我要处理数周.
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..
代码如下:
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 :)
也许可以基于Calendar类 https://docs.microsoft.com/zh-cn/dotnet/api/system.globalization.calendar?view=netframework-4.8
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..
谢谢大家
欢呼
推荐答案
您可以编写一些通用扩展来对此进行简单计算.
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);
这篇关于如何获得基于年周数的月周数(即第33周)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!