根据周数计算日期
任何人都知道一种简单的方法来获取一周第一天的日期(欧洲的星期一)。 我知道年份和周数吗? 我将在 C# 中执行此操作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
任何人都知道一种简单的方法来获取一周第一天的日期(欧洲的星期一)。 我知道年份和周数吗? 我将在 C# 中执行此操作。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
最简单的方法可能是找到一年中的第一个星期一,然后添加相关的周数。 这是一些示例代码。 顺便说一句,它假定周数从 1 开始:
The easiest way is probably to find the first Monday of the year, and then add the relevant number of weeks. Here's some sample code. It assumes a week number starting at 1, by the way:
就我个人而言,我会利用文化信息来获取一周中的某一天,并循环到文化的一周第一天。 我不确定我的解释是否正确,这是一个例子:
Personally I'd take advantage of the culture info to get the day of the week and loop down to the culture's first day of the week. I'm not sure if I'm explaining it properly, here's an example:
使用 Fluent DateTime http://fluentdatetime.codeplex.com/
using Fluent DateTime http://fluentdatetime.codeplex.com/
这是一种与 Google Analytics 的周数兼容的方法,也与我们在英特尔内部使用的相同编号方案兼容,我确信它也在许多其他环境中使用。
Here is a method that is compatible with the week numbers that Google Analytics, and also the same numbering scheme we used internally at Intel, and which I'm sure is also used in a lot of other contexts.
稍微修改了 Mikael Svenson 代码。 我找到了第一个星期一所在的周并适当更改了周数。
Lightly changed Mikael Svenson code. I found the week of the first monday and appropriate change the week number.
根据瑞典使用的 ISO 8601:1988 标准,一年中的第一周是新的一年中至少有四天的第一周。
因此,如果您的一周从星期一开始,则任何一年的第一个星期四都在第一周内。
您可以从中添加 DateAdd 或 DateDiff 。
According to ISO 8601:1988 that is used in Sweden the first week of the year is the first week that has at least four days within the new year.
So if your week starts on a Monday the first Thursday any year is within the first week.
You can DateAdd or DateDiff from that.
假设周数从 1 开始,
这将为您提供任何给定周的第一天。 我没有对其进行大量测试,但看起来它确实有效。 它比我在网上找到的大多数其他解决方案都要小,所以想分享。
Assuming the week number starts at 1
This should give you the first day in any given week. I haven't done a lot of testing on it, but looks like it works. It's smaller solution than most other's I found on the web, so wanted to share.
免费的.NET 时间段库包括ISO 8601 符合类 Week:
The free Time Period Library for .NET includes the ISO 8601 conform class Week:
这个对我有用,它还有一个优点是期望以文化信息作为参数来测试不同文化的公式。 如果为空,它将获取当前的文化信息...有效值如下:“it”,“en-us”,“fr”,...等等。
诀窍是减去一年中第一天的周数,这可能是 1,表示第一天在第一周之内。 希望这可以帮助。
This one worked for me, it also have the advantage of expecting a cultureinfo as parameter to test the formula with different cultures. If empty, it gets the current culture info... valid values are like: "it", "en-us", "fr", ... ando so on.
The trick is to subtract the week number of the first day of the year, that may be 1 to indicate that the first day is within the first week. Hope this helps.
我尝试了上面的一些代码,有些有小错误,当你尝试不同的年份和不同的一周开始日期时,你会看到它们,我采用了 Jon Skeet 的代码,修复它并且它可以工作,非常简单的代码。
I tried some codes above and some have small mistakes, when you try different years with different starting days of week you will see them, I took the code of Jon Skeet, fix it and it works, very simple code.
我简化了 Mikael Svensson 提供的代码,该代码对于欧洲许多国家/地区都是正确的。
I simplified the code Mikael Svensson provided which is correct for many countries in Europe.
正如 khellang 所建议的(抱歉,我无法添加评论),使用 .NET Core 非常简单。 这是我的一段代码:
As suggested by khellang (sorry but I cannot add comments), with .NET Core is quite simple. This is my piece of code:
第 1 周定义为从星期一开始并包含一年中第一个星期四的一周。
Week 1 is defined as being the week that starts on a Monday and contains the first Thursday of the year.
要双向转换,请参阅此处:有关 ISO 周日期的维基百科文章
To convert in both directions, see here: Wikipedia article on ISO week dates
我通过覆盖对托马斯的解决方案进行了一些改进:
否则日期提前一周。
谢谢托马斯,很有帮助。
I improved a little on Thomas' solution with an override:
Otherwise the date was preceding by one week..
Thank you Thomas, great help.
我使用了其中一个解决方案,但它给了我错误的结果,仅仅是因为它将星期日算作一周的第一天。
我将: 更改
为:
现在它正在发挥作用。
I used one of the solutions but it gave me wrong results, simply because it counts Sunday as a first day of the week.
I changed:
to:
and now it is working as a charm.
建议的解决方案并不完整 - 它仅适用于 CalendarWeekRule.FirstFullWeek。 其他类型的周规则不起作用。 使用这个测试用例可以看出这一点:
The proposed solution is not complete - it only works for CalendarWeekRule.FirstFullWeek. Other types of week rules do not work. This can be seen using this test case:
我对所提出的解决方案做了一个改进版本,它更简单,并且参数化了firstDayOfWeek:
I have made a refined version of the proposed solution that is a simpler and parametrises the firstDayOfWeek:
当我们想要计算给定年份、周数和星期几的日期时,这是我的解决方案。
this is my solution when we want to calculate a date given year, week number and day of the week.
我已经编写并测试了以下代码,并且对我来说工作得很好。 如果有人遇到此问题,请告诉我,我也发布了一个问题,以便获得最佳答案。 有人可能会发现它很有用。
I have written and tested the following code and is working perfectly fine for me. Please let me know if anyone face trouble with this, I have posted a question as well in order to get the best possible answer. Someone may find it useful.
目前,没有 C# 类可以正确处理 ISO 8601week 数字。 尽管您可以实例化一种文化,寻找最接近的事物并纠正它,但我认为最好自己进行完整的计算:
Currently, there is no C# class that correctly handles ISO 8601week numbers. Even though you can instantiate a culture, look for the closest thing and correct that, I think it is better to do the complete calculation yourself:
我发现的最大问题之一是将周转换为日期,然后从日期转换为周。
主要问题是当尝试从属于上一年一周的日期获取正确的星期年份时。 幸运的是 System.Globalization.ISOWeek.GetYear 可以处理这个问题。
这是我的解决方案:
第二大问题是美国偏好从周日开始的几周。
我从上面提出了子类
WeekOfYear
的解决方案,并管理构造函数中的偏移量(将周转换为日期)和DateToWeekOfYear
(将日期转换为周) )。这是一些测试代码:
One of the biggest problems I found was to convert from weeks to dates, and then from dates to weeks.
The main problem is when trying to get the correct week year from a date that belongs to a week of the previous year. Luckily
System.Globalization.ISOWeek.GetYear
handles this.Here is my solution:
The second biggest problem was the preference for weeks starting on Sundays in the US.
The solution I cam up with subclasses
WeekOfYear
from above, and manages the offset of the in the constructor (which converts week to dates) andDateToWeekOfYear
(which converts from date to week).Here is some test code:
即使 @RobinAndersson 进行了修复,我也对 @HenkHolterman 的解决方案有疑问。
阅读 ISO 8601 标准可以很好地解决这个问题。 使用第一个星期四作为目标,而不是星期一。 下面的代码也适用于 2009 年第 53 周。
I had issues with the solution by @HenkHolterman even with the fix by @RobinAndersson.
Reading up on the ISO 8601 standard resolves the issue nicely. Use the first Thursday as the target and not Monday. The code below will work for Week 53 of 2009 as well.
注意
以下答案使用 .NET 日历规则。 它不承诺符合 ISO8601。 当您需要时,请参阅此处的一些其他答案。 周编号是一团糟,总是先尝试找出需要遵循的规则。
下面的代码正确地将 2009 年第 1 周的开始时间定为 2008 年 12 月 29 日。 CalendarWeekRule 可能应该是一个参数。
请注意,weekNum 应该 >= 1
Note
The below answer uses the .NET Calendar rules. It does not promise ISO8601 conformance. See some of the other answers here when you need that. Week numbering is a mess, always try to find out what rules you need to follow first.
The code below correctly puts the start of week 1, 2009 at 29-12-2008. The CalendarWeekRule probably should be a parameter.
Note that the weekNum should be >= 1
更新:.NET Core 3.0 和 .NET Standard 2.1 已随此类型一起提供。
好消息! 将
System.Globalization.ISOWeek
添加到 .NET 的拉取请求 Core 刚刚合并,目前计划发布 3.0 版本。 希望它能在不久的将来传播到其他 .NET 平台。您应该能够使用 ISOWeek.ToDateTime(intyear, int week, DayOfWeek dayOfWeek) 方法来计算此值。
您可以找到源代码 此处。
UPDATE: .NET Core 3.0 and .NET Standard 2.1 has shipped with this type.
Good news! A pull request adding
System.Globalization.ISOWeek
to .NET Core was just merged and is currently slated for the 3.0 release. Hopefully it will propagate to the other .NET platforms in a not-too-distant future.You should be able to use the
ISOWeek.ToDateTime(int year, int week, DayOfWeek dayOfWeek)
method to calculate this.You can find the source code here.
我喜欢 Henk Holterman 提供的解决方案。 但为了更加独立于文化,您必须获得当前文化的一周的第一天(并不总是星期一):
I like the solution provided by Henk Holterman. But to be a little more culture independent, you have to get the first day of the week for the current culture ( it's not always monday ):