C# 从周数计算日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/662379/
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
Calculate date from week number
提问by Bobby
Anyone know an easy way to get the date of the first day in the week (monday here in Europe). I know the year and the week number? I'm going to do this in C#.
任何人都知道一种简单的方法来获取一周中第一天的日期(欧洲的星期一)。我知道年份和星期几?我将在 C# 中执行此操作。
采纳答案by Mikael Svenson
I had issues with the solution by @HenkHolterman even with the fix by @RobinAndersson.
即使@RobinAndersson 进行了修复,我对@HenkHolterman 的解决方案也有问题。
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.
阅读 ISO 8601 标准可以很好地解决这个问题。使用第一个星期四作为目标,而不是星期一。下面的代码也适用于 2009 年的第 53 周。
public static DateTime FirstDateOfWeekISO8601(int year, int weekOfYear)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
// Use first Thursday in January to get first week of the year as
// it will never be in Week 52/53
DateTime firstThursday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = weekOfYear;
// As we're adding days to a date in Week 1,
// we need to subtract 1 in order to get the right date for week #1
if (firstWeek == 1)
{
weekNum -= 1;
}
// Using the first Thursday as starting week ensures that we are starting in the right year
// then we add number of weeks multiplied with days
var result = firstThursday.AddDays(weekNum * 7);
// Subtract 3 days from Thursday to get Monday, which is the first weekday in ISO8601
return result.AddDays(-3);
}
回答by Jon Skeet
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:
最简单的方法大概就是找出一年中的第一个星期一,然后加上相关的周数。这是一些示例代码。顺便说一下,它假定周数从 1 开始:
using System;
class Test
{
static void Main()
{
// Show the third Tuesday in 2009. Should be January 20th
Console.WriteLine(YearWeekDayToDateTime(2009, DayOfWeek.Tuesday, 3));
}
static DateTime YearWeekDayToDateTime(int year, DayOfWeek day, int week)
{
DateTime startOfYear = new DateTime (year, 1, 1);
// The +7 and %7 stuff is to avoid negative numbers etc.
int daysToFirstCorrectDay = (((int)day - (int)startOfYear.DayOfWeek) + 7) % 7;
return startOfYear.AddDays(7 * (week-1) + daysToFirstCorrectDay);
}
}
回答by amaca
Week 1 is defined as being the week that starts on a Monday and contains the first Thursday of the year.
第 1 周定义为从星期一开始并包含一年中的第一个星期四的那一周。
回答by Alexander Kahoun
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:
就我个人而言,我会利用文化信息来获取一周中的哪一天并循环到该文化的第一天。我不确定我的解释是否正确,这是一个例子:
public DateTime GetFirstDayOfWeek(int year, int weekNumber)
{
return GetFirstDayOfWeek(year, weekNumber, Application.CurrentCulture);
}
public DateTime GetFirstDayOfWeek(int year, int weekNumber,
System.Globalization.CultureInfo culture)
{
System.Globalization.Calendar calendar = culture.Calendar;
DateTime firstOfYear = new DateTime(year, 1, 1, calendar);
DateTime targetDay = calendar.AddWeeks(firstOfYear, weekNumber);
DayOfWeek firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek;
while (targetDay.DayOfWeek != firstDayOfWeek)
{
targetDay = targetDay.AddDays(-1);
}
return targetDay;
}
回答by idstam
According to ISO 8601:1988 that is used in Swedenthe first week of the year is the first week that has at least four days within the new year.
根据瑞典使用的ISO 8601:1988 ,一年中的第一周是新年中至少有四天的第一周。
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.
因此,如果您的一周从星期一开始,则任何一年的第一个星期四都在第一周内。您可以从中使用 DateAdd 或 DateDiff。
回答by Thomas
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 ):
我喜欢 Henk Holterman 提供的解决方案。但是要更加独立于文化,您必须获得当前文化的一周的第一天(并不总是 monday ):
using System.Globalization;
static DateTime FirstDateOfWeek(int year, int weekOfYear)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
int firstWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(jan1, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
if (firstWeek <= 1)
{
weekOfYear -= 1;
}
return firstMonday.AddDays(weekOfYear * 7);
}
回答by Simon
using Fluent DateTime http://fluentdatetime.codeplex.com/
使用 Fluent DateTime http://fluentdatetime.codeplex.com/
var year = 2009;
var firstDayOfYear = new DateTime(year, 1, 1);
var firstMonday = firstDayOfYear.Next(DayOfWeek.Monday);
var weeksDateTime = 12.Weeks().Since(firstMonday);
回答by Robert L
To convert in both directions, see here: Wikipedia article on ISO week dates
要双向转换,请参见此处:关于 ISO 周日期的维基百科文章
回答by QuinnG
Assuming the week number starts at 1
假设周数从 1 开始
DateTime dt = new DateTime(YearNumber, 1, 1).AddDays((WeekNumber - 1) * 7 - (WeekNumber == 1 ? 0 : 1));
return dt.AddDays(-(int)dt.DayOfWeek);
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.
这应该为您提供任何给定周的第一天。我没有对它进行很多测试,但看起来它有效。它比我在网上找到的大多数其他解决方案都要小,所以想分享一下。
回答by pasx
I improved a little on Thomas' solution with an override:
我通过覆盖对 Thomas 的解决方案进行了一些改进:
public static DateTime FirstDateOfWeek(int year, int weekOfYear)
{
return Timer.FirstDateOfWeekOfMonth(year, 1, weekOfYear);
}
public static DateTime FirstDateOfWeekOfMonth(int year, int month,
int weekOfYear)
{
DateTime dtFirstDayOfMonth = new DateTime(year, month, 1);
//I also commented out this part:
/*
if (firstWeek <= 1)
{
weekOfYear -= 1;
}
*/
Otherwise the date was preceding by one week..
否则日期会提前一周..
Thank you Thomas, great help.
谢谢托马斯,很有帮助。
回答by pasx
The free Time Period Library for .NETincludes the ISO 8601conform class Week:
.NET的免费时间段库包括符合ISO 8601 的类Week:
// ----------------------------------------------------------------------
public static DateTime GetFirstDayOfWeek( int year, int weekOfYear )
{
return new Week( year, weekOfYear ).FirstDayStart;
} // GetFirstDayOfWeek