I have Googled a lot and found a lot of solutions, but none of them give me the correct week number for the 2012-12-31. Even the example on MSDN (link) fails.
我在谷歌上搜索了很多,找到了很多解决方案,但没有一个能给出2012-12-31年度的正确周数。即使是MSDN(链接)上的示例也会失败。
2012-12-31 is Monday, therefore it should be Week 1, but every method I tried gives me 53. Here are some of the methods, that I have tried:
2012-12-31是周一,所以应该是第一周,但是我尝试的每一种方法都给了我53。以下是我尝试过的一些方法:
From the MDSN Library:
从MDSN库:
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;
return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
Solution 2:
解决方案2:
return new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Solution 3:
解决方案3:
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
Update
更新
The following method actually returns 1 when date is 2012-12-31. In other words, my problem was that my methods were not following the ISO-8601 standard.
下面的方法实际上在日期为2012-12-31时返回1。换句话说,我的问题是我的方法没有遵循ISO-8601标准。
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
10 个解决方案
#1
211
As noted in this MSDN page there is a slight difference between ISO8601 week and .Net week numbering.
正如在MSDN页面中所指出的,ISO8601周和。net周编号之间有细微的区别。
You can refer to this article in MSDN Blog for a better explanation: "ISO 8601 Week of Year format in Microsoft .Net"
您可以参考MSDN博客上的这篇文章来获得更好的解释:“在Microsoft .Net中使用ISO 8601周的一年格式”
Simply put, .Net allow weeks to be split across years while the ISO standard does not. In the article there is also a simple function to get the correct ISO 8601 week number for the last week of the year.
简而言之,. net允许几周被分割成几年,而ISO标准没有。在本文中,还有一个简单的函数,用于获取一年最后一周的正确ISO 8601周号。
Update The following method actually returns 1 for 2012-12-31
which is correct in ISO 8601 (e.g. Germany).
更新以下方法实际上返回1 for 2012-12-31,这在ISO 8601(例如,德国)中是正确的。
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
#2
23
There can be more than 52 weeks in a year. Each year has 52 full weeks + 1 or +2 (leap year) days extra. They make up for a 53th week.
一年有超过52周的时间。每年有52个完整的星期+ 1或+2(闰年)额外的天数。他们弥补了第53个星期。
- 52 weeks * 7days = 364 days.
- 52周* 7天= 364天。
So for each year you have at least one an extra day. Two for leap years. Are these extra days counted as separate weeks of their own?
因此,每年你至少有一个额外的一天。两个闰年。这些额外的日子是不是属于他们自己的几个星期呢?
How many weeks there are really depends on the starting day of your week. Let's consider this for 2012.
有多少周取决于你一周的开始。让我们考虑一下2012年的情况。
- US (Sunday -> Saturday): 52 weeks + one short 2 day week for 2012-12-30 & 2012-12-31. This results in a total of 53 weeks. Last two days of this year (Sunday + Monday) make up their own short week.
- 美国(周日->周六):52周+ 2012-12-30和2012-12-31短2周。结果总共是53周。今年的最后两天(周日+周一)弥补了他们自己的短暂一周。
Check your current Culture's settings to see what it uses as the first day of the week.
检查你当前文化的设置,看看它在一周的第一天是如何使用的。
As you see it's normal to get 53 as a result.
就像你看到的,结果是53。
- Europe (Monday -> Sunday): January 2dn (2012-1-2) is the first monday, so this is the first day of the first week. Ask the week number for the 1st of January and you'll get back 52 as it is considered part of 2011 last's week.
- 欧洲(周一->周日):1月2dn(2012-1-2)是第一个周一,所以这是第一周的第一天。问一下1月1日的周数,你会得到52,因为它被认为是2011年最后一周的一部分。
It's even possible to have a 54th week. Happens every 28 years when the 1st of January and the 31st of December are treated as separate weeks. It must be a leap year too.
第54周也是可能的。每28年发生一次,1月1日和12月31日是分开的几周。那一定也是闰年。
For example, the year 2000 had 54 weeks. January 1st (sat) was the first one week day, and 31st December (sun) was the second one week day.
例如,2000年有54周。1月1日(sat)是第一个一周,12月31日(sun)是第二个一周。
var d = new DateTime(2012, 12, 31);
CultureInfo cul = CultureInfo.CurrentCulture;
var firstDayWeek = cul.Calendar.GetWeekOfYear(
d,
CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int weekNum = cul.Calendar.GetWeekOfYear(
d,
CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int year = weekNum == 52 && d.Month == 1 ? d.Year - 1 : d.Year;
Console.WriteLine("Year: {0} Week: {1}", year, weekNum);
Prints out: Year: 2012 Week: 54
打印出来:年份:2012周:54
Change CalendarWeekRule in the above example to FirstFullWeek or FirstFourDayWeek and you'll get back 53. Let's keep the start day on Monday since we are dealing with Germany.
将上面示例中的calendar arweekrule更改为FirstFullWeek或FirstFourDayWeek,您将返回53。因为我们要和德国打交道,所以我们星期一就开始上班吧。
So week 53 starts on monday 2012-12-31, lasts one day and then stops.
所以第53周从2012-12-31周一开始,持续一天,然后停止。
53 is the correct answer. Change the Culture to germany if want to to try it.
53是正确答案。如果你想尝试一下德国文化,那就改变一下吧。
CultureInfo cul = CultureInfo.GetCultureInfo("de-DE");
#3
9
This is the way:
这是道:
public int GetWeekNumber()
{
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
}
Most important for is the CalendarWeekRule
parameter.
最重要的是calendar arweekrule参数。
在这里看到的:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=IT-IT&k=k(System.Globalization.CalendarWeekRule);k(TargetFrameworkMoniker -.NETFramework
#4
5
I'm going to play Necromancer here :-)
我将在这里扮演亡灵法师:
Since there doesn't seem to be a .Net-culture that yields the correct ISO-8601 week number, I'd rater bypass the built in week determination alltogether, and do the calculation manually, instead of atempting to correct a partially correct result.
由于似乎没有一个。net文化可以产生正确的ISO-8601周,所以我建议把构建的in week确定放在一起,手工计算,而不是自动修正部分正确的结果。
What I ended up with is the following extension method:
我最后得到的是以下扩展方法:
/// <summary>
/// Converts a date to a week number.
/// ISO 8601 week 1 is the week that contains the first Thursday that year.
/// </summary>
public static int ToIso8601Weeknumber(this DateTime date)
{
var thursday = date.AddDays(3 - date.DayOfWeek.DayOffset());
return (thursday.DayOfYear - 1) / 7 + 1;
}
/// <summary>
/// Converts a week number to a date.
/// Note: Week 1 of a year may start in the previous year.
/// ISO 8601 week 1 is the week that contains the first Thursday that year, so
/// if December 28 is a Monday, December 31 is a Thursday,
/// and week 1 starts January 4.
/// If December 28 is a later day in the week, week 1 starts earlier.
/// If December 28 is a Sunday, it is in the same week as Thursday January 1.
/// </summary>
public static DateTime FromIso8601Weeknumber(int weekNumber, int? year = null, DayOfWeek day = DayOfWeek.Monday)
{
var dec28 = new DateTime((year ?? DateTime.Today.Year) - 1, 12, 28);
var monday = dec28.AddDays(7 * weekNumber - dec28.DayOfWeek.DayOffset());
return monday.AddDays(day.DayOffset());
}
/// <summary>
/// Iso8601 weeks start on Monday. This returns 0 for Monday.
/// </summary>
private static int DayOffset(this DayOfWeek weekDay)
{
return ((int)weekDay + 6) % 7;
}
First of all, ((int)date.DayOfWeek + 6) % 7) determines the weekday number, 0=monday, 6=sunday.
首先,((int)日期。7)确定工作日数,0=周一,6=周日。
date.AddDays(-((int)date.DayOfWeek + 6) % 7) determines the date of the monday preceiding the requested week number.
date.AddDays(-((int)日期。每星期的天数+ 6)% 7)确定在要求的周数之前的星期一的日期。
Three days later is the target thursday, which determines what year the week is in.
三天后是周四的目标,这决定了一周的时间。
If you divide the (zero based) day-number within the year by seven (round down), you get the (zero based) week number in the year.
如果你把一年内的(以零为基础的)日数除以7(四舍五入),你会得到一年中的(以零为基础的)周数。
In c#, integer calculation results are round down implicitly.
在c#中,整数的计算结果是隐式的。
#5
3
C# to Powershell port from code above from il_guru:
从il_guru上面的代码到Powershell端口:
function GetWeekOfYear([datetime] $inputDate)
{
$day = [System.Globalization.CultureInfo]::InvariantCulture.Calendar.GetDayOfWeek($inputDate)
if (($day -ge [System.DayOfWeek]::Monday) -and ($day -le [System.DayOfWeek]::Wednesday))
{
$inputDate = $inputDate.AddDays(3)
}
# Return the week of our adjusted day
$weekofYear = [System.Globalization.CultureInfo]::InvariantCulture.Calendar.GetWeekOfYear($inputDate, [System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [System.DayOfWeek]::Monday)
return $weekofYear
}
#6
2
The easiest way to determine the week number ISO 8601 style using c# and the DateTime class.
使用c#和DateTime类确定周数ISO 8601样式的最简单方法。
Ask this: the how-many-eth thursday of the year is the thursday of this week. The answer equals the wanted week number.
问问这个:一年中的“多好”星期四是本周的星期四。答案等于所需周数。
var dayOfWeek = (int)moment.DayOfWeek;
// Make monday the first day of the week
if (--dayOfWeek < 0)
dayOfWeek = 6;
// The whole nr of weeks before this thursday plus one is the week number
var weekNumber = (moment.AddDays(3 - dayOfWeek).DayOfYear - 1) / 7 + 1;
#7
1
var cultureInfo = CultureInfo.CurrentCulture;
var calendar = cultureInfo.Calendar;
var calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule;
var firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
var lastDayOfWeek = cultureInfo.LCID == 1033 //En-us
? DayOfWeek.Saturday
: DayOfWeek.Sunday;
var lastDayOfYear = new DateTime(date.Year, 12, 31);
var weekNumber = calendar.GetWeekOfYear(date, calendarWeekRule, firstDayOfWeek);
//Check if this is the last week in the year and it doesn`t occupy the whole week
return weekNumber == 53 && lastDayOfYear.DayOfWeek != lastDayOfWeek
? 1
: weekNumber;
It works well both for US and Russian cultures. ISO 8601 also will be correct, `cause Russian week starts at Monday.
它对美国和俄罗斯文化都很有效。ISO 8601也是正确的,因为俄罗斯周从周一开始。
#8
0
A year has 52 weeks and 1 day or 2 in case of a lap year (52 x 7 = 364). 2012-12-31 would be week 53, a week that would only have 2 days because 2012 is a lap year.
一年有52周1天或2天(52 x 7 = 364)。2012-12-31将是第53周,一周只有2天,因为2012年是一圈一年。
#9
0
The question is: How do you define if a week is in 2012 or in 2013? Your supposition, I guess, is that since 6 days of the week are in 2013, this week should be marked as the first week of 2013.
问题是:你如何定义一个星期是在2012年还是在2013年?我猜你的猜测是,由于2013年是每周6天,本周应该被标记为2013年的第一周。
Not sure if this is the right way to go. That week started on 2012 (On monday 31th Dec), so it should be marked as the last week of 2012, therefore it should be the 53rd of 2012. The first week of 2013 should start on monday the 7th.
不确定这是不是正确的方法。那一周从2012年开始(12月31日星期一),所以应该是2012年的最后一周,所以应该是2012年的第53周。2013年的第一周应该从7号星期一开始。
Now, you can handle the particular case of edge weeks (first and last week of the year) using the day of week information. It all depends on your logic.
现在,您可以使用周日信息处理edge weeks(一年中的第一个和最后一个星期)的特定情况。这一切都取决于你的逻辑。
#10
0
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date1 = new DateTime(2011, 1, 1);
Calendar cal = dfi.Calendar;
Console.WriteLine("{0:d}: Week {1} ({2})", date1,
cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,
dfi.FirstDayOfWeek),
cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));
#1
211
As noted in this MSDN page there is a slight difference between ISO8601 week and .Net week numbering.
正如在MSDN页面中所指出的,ISO8601周和。net周编号之间有细微的区别。
You can refer to this article in MSDN Blog for a better explanation: "ISO 8601 Week of Year format in Microsoft .Net"
您可以参考MSDN博客上的这篇文章来获得更好的解释:“在Microsoft .Net中使用ISO 8601周的一年格式”
Simply put, .Net allow weeks to be split across years while the ISO standard does not. In the article there is also a simple function to get the correct ISO 8601 week number for the last week of the year.
简而言之,. net允许几周被分割成几年,而ISO标准没有。在本文中,还有一个简单的函数,用于获取一年最后一周的正确ISO 8601周号。
Update The following method actually returns 1 for 2012-12-31
which is correct in ISO 8601 (e.g. Germany).
更新以下方法实际上返回1 for 2012-12-31,这在ISO 8601(例如,德国)中是正确的。
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
#2
23
There can be more than 52 weeks in a year. Each year has 52 full weeks + 1 or +2 (leap year) days extra. They make up for a 53th week.
一年有超过52周的时间。每年有52个完整的星期+ 1或+2(闰年)额外的天数。他们弥补了第53个星期。
- 52 weeks * 7days = 364 days.
- 52周* 7天= 364天。
So for each year you have at least one an extra day. Two for leap years. Are these extra days counted as separate weeks of their own?
因此,每年你至少有一个额外的一天。两个闰年。这些额外的日子是不是属于他们自己的几个星期呢?
How many weeks there are really depends on the starting day of your week. Let's consider this for 2012.
有多少周取决于你一周的开始。让我们考虑一下2012年的情况。
- US (Sunday -> Saturday): 52 weeks + one short 2 day week for 2012-12-30 & 2012-12-31. This results in a total of 53 weeks. Last two days of this year (Sunday + Monday) make up their own short week.
- 美国(周日->周六):52周+ 2012-12-30和2012-12-31短2周。结果总共是53周。今年的最后两天(周日+周一)弥补了他们自己的短暂一周。
Check your current Culture's settings to see what it uses as the first day of the week.
检查你当前文化的设置,看看它在一周的第一天是如何使用的。
As you see it's normal to get 53 as a result.
就像你看到的,结果是53。
- Europe (Monday -> Sunday): January 2dn (2012-1-2) is the first monday, so this is the first day of the first week. Ask the week number for the 1st of January and you'll get back 52 as it is considered part of 2011 last's week.
- 欧洲(周一->周日):1月2dn(2012-1-2)是第一个周一,所以这是第一周的第一天。问一下1月1日的周数,你会得到52,因为它被认为是2011年最后一周的一部分。
It's even possible to have a 54th week. Happens every 28 years when the 1st of January and the 31st of December are treated as separate weeks. It must be a leap year too.
第54周也是可能的。每28年发生一次,1月1日和12月31日是分开的几周。那一定也是闰年。
For example, the year 2000 had 54 weeks. January 1st (sat) was the first one week day, and 31st December (sun) was the second one week day.
例如,2000年有54周。1月1日(sat)是第一个一周,12月31日(sun)是第二个一周。
var d = new DateTime(2012, 12, 31);
CultureInfo cul = CultureInfo.CurrentCulture;
var firstDayWeek = cul.Calendar.GetWeekOfYear(
d,
CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int weekNum = cul.Calendar.GetWeekOfYear(
d,
CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int year = weekNum == 52 && d.Month == 1 ? d.Year - 1 : d.Year;
Console.WriteLine("Year: {0} Week: {1}", year, weekNum);
Prints out: Year: 2012 Week: 54
打印出来:年份:2012周:54
Change CalendarWeekRule in the above example to FirstFullWeek or FirstFourDayWeek and you'll get back 53. Let's keep the start day on Monday since we are dealing with Germany.
将上面示例中的calendar arweekrule更改为FirstFullWeek或FirstFourDayWeek,您将返回53。因为我们要和德国打交道,所以我们星期一就开始上班吧。
So week 53 starts on monday 2012-12-31, lasts one day and then stops.
所以第53周从2012-12-31周一开始,持续一天,然后停止。
53 is the correct answer. Change the Culture to germany if want to to try it.
53是正确答案。如果你想尝试一下德国文化,那就改变一下吧。
CultureInfo cul = CultureInfo.GetCultureInfo("de-DE");
#3
9
This is the way:
这是道:
public int GetWeekNumber()
{
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
}
Most important for is the CalendarWeekRule
parameter.
最重要的是calendar arweekrule参数。
在这里看到的:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=IT-IT&k=k(System.Globalization.CalendarWeekRule);k(TargetFrameworkMoniker -.NETFramework
#4
5
I'm going to play Necromancer here :-)
我将在这里扮演亡灵法师:
Since there doesn't seem to be a .Net-culture that yields the correct ISO-8601 week number, I'd rater bypass the built in week determination alltogether, and do the calculation manually, instead of atempting to correct a partially correct result.
由于似乎没有一个。net文化可以产生正确的ISO-8601周,所以我建议把构建的in week确定放在一起,手工计算,而不是自动修正部分正确的结果。
What I ended up with is the following extension method:
我最后得到的是以下扩展方法:
/// <summary>
/// Converts a date to a week number.
/// ISO 8601 week 1 is the week that contains the first Thursday that year.
/// </summary>
public static int ToIso8601Weeknumber(this DateTime date)
{
var thursday = date.AddDays(3 - date.DayOfWeek.DayOffset());
return (thursday.DayOfYear - 1) / 7 + 1;
}
/// <summary>
/// Converts a week number to a date.
/// Note: Week 1 of a year may start in the previous year.
/// ISO 8601 week 1 is the week that contains the first Thursday that year, so
/// if December 28 is a Monday, December 31 is a Thursday,
/// and week 1 starts January 4.
/// If December 28 is a later day in the week, week 1 starts earlier.
/// If December 28 is a Sunday, it is in the same week as Thursday January 1.
/// </summary>
public static DateTime FromIso8601Weeknumber(int weekNumber, int? year = null, DayOfWeek day = DayOfWeek.Monday)
{
var dec28 = new DateTime((year ?? DateTime.Today.Year) - 1, 12, 28);
var monday = dec28.AddDays(7 * weekNumber - dec28.DayOfWeek.DayOffset());
return monday.AddDays(day.DayOffset());
}
/// <summary>
/// Iso8601 weeks start on Monday. This returns 0 for Monday.
/// </summary>
private static int DayOffset(this DayOfWeek weekDay)
{
return ((int)weekDay + 6) % 7;
}
First of all, ((int)date.DayOfWeek + 6) % 7) determines the weekday number, 0=monday, 6=sunday.
首先,((int)日期。7)确定工作日数,0=周一,6=周日。
date.AddDays(-((int)date.DayOfWeek + 6) % 7) determines the date of the monday preceiding the requested week number.
date.AddDays(-((int)日期。每星期的天数+ 6)% 7)确定在要求的周数之前的星期一的日期。
Three days later is the target thursday, which determines what year the week is in.
三天后是周四的目标,这决定了一周的时间。
If you divide the (zero based) day-number within the year by seven (round down), you get the (zero based) week number in the year.
如果你把一年内的(以零为基础的)日数除以7(四舍五入),你会得到一年中的(以零为基础的)周数。
In c#, integer calculation results are round down implicitly.
在c#中,整数的计算结果是隐式的。
#5
3
C# to Powershell port from code above from il_guru:
从il_guru上面的代码到Powershell端口:
function GetWeekOfYear([datetime] $inputDate)
{
$day = [System.Globalization.CultureInfo]::InvariantCulture.Calendar.GetDayOfWeek($inputDate)
if (($day -ge [System.DayOfWeek]::Monday) -and ($day -le [System.DayOfWeek]::Wednesday))
{
$inputDate = $inputDate.AddDays(3)
}
# Return the week of our adjusted day
$weekofYear = [System.Globalization.CultureInfo]::InvariantCulture.Calendar.GetWeekOfYear($inputDate, [System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [System.DayOfWeek]::Monday)
return $weekofYear
}
#6
2
The easiest way to determine the week number ISO 8601 style using c# and the DateTime class.
使用c#和DateTime类确定周数ISO 8601样式的最简单方法。
Ask this: the how-many-eth thursday of the year is the thursday of this week. The answer equals the wanted week number.
问问这个:一年中的“多好”星期四是本周的星期四。答案等于所需周数。
var dayOfWeek = (int)moment.DayOfWeek;
// Make monday the first day of the week
if (--dayOfWeek < 0)
dayOfWeek = 6;
// The whole nr of weeks before this thursday plus one is the week number
var weekNumber = (moment.AddDays(3 - dayOfWeek).DayOfYear - 1) / 7 + 1;
#7
1
var cultureInfo = CultureInfo.CurrentCulture;
var calendar = cultureInfo.Calendar;
var calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule;
var firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
var lastDayOfWeek = cultureInfo.LCID == 1033 //En-us
? DayOfWeek.Saturday
: DayOfWeek.Sunday;
var lastDayOfYear = new DateTime(date.Year, 12, 31);
var weekNumber = calendar.GetWeekOfYear(date, calendarWeekRule, firstDayOfWeek);
//Check if this is the last week in the year and it doesn`t occupy the whole week
return weekNumber == 53 && lastDayOfYear.DayOfWeek != lastDayOfWeek
? 1
: weekNumber;
It works well both for US and Russian cultures. ISO 8601 also will be correct, `cause Russian week starts at Monday.
它对美国和俄罗斯文化都很有效。ISO 8601也是正确的,因为俄罗斯周从周一开始。
#8
0
A year has 52 weeks and 1 day or 2 in case of a lap year (52 x 7 = 364). 2012-12-31 would be week 53, a week that would only have 2 days because 2012 is a lap year.
一年有52周1天或2天(52 x 7 = 364)。2012-12-31将是第53周,一周只有2天,因为2012年是一圈一年。
#9
0
The question is: How do you define if a week is in 2012 or in 2013? Your supposition, I guess, is that since 6 days of the week are in 2013, this week should be marked as the first week of 2013.
问题是:你如何定义一个星期是在2012年还是在2013年?我猜你的猜测是,由于2013年是每周6天,本周应该被标记为2013年的第一周。
Not sure if this is the right way to go. That week started on 2012 (On monday 31th Dec), so it should be marked as the last week of 2012, therefore it should be the 53rd of 2012. The first week of 2013 should start on monday the 7th.
不确定这是不是正确的方法。那一周从2012年开始(12月31日星期一),所以应该是2012年的最后一周,所以应该是2012年的第53周。2013年的第一周应该从7号星期一开始。
Now, you can handle the particular case of edge weeks (first and last week of the year) using the day of week information. It all depends on your logic.
现在,您可以使用周日信息处理edge weeks(一年中的第一个和最后一个星期)的特定情况。这一切都取决于你的逻辑。
#10
0
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date1 = new DateTime(2011, 1, 1);
Calendar cal = dfi.Calendar;
Console.WriteLine("{0:d}: Week {1} ({2})", date1,
cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,
dfi.FirstDayOfWeek),
cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));