"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
CKKwan wrote:
>Anybody know how can I get the Work Week of a given date using CSharp?
Example, in SQL Server we can get using DATEPART(ww, date); I need
something equavalent.
You already got a couple of replies pointing you to
Calendar.GetWeekOfYear - but I will point out that
if you are in Europe or other places that uses ISO
weeks then that return wrong result in a few cases
(at least at all Windows and .NET versions I have tried).
It is easy to code a solution though.
Arne
The bug (and workaround) that Arne is refering to is described in KB article
Q200299 which you can find here:
http://support.microsoft.com/kb/200299
Here's the code for it:
DateTimeFormatInfo dateTimeFormat =
CultureInfo.CurrentCulture.DateTimeFormat;
int week = dateTimeFormat.Calendar.GetWeekOfYear(yourDate,
dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
int nextWeek = dateTimeFormat.Calendar.GetWeekOfYear(yourDate.Add Days(7),
dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
if (week == 53 && nextWeek == 2)
{
week = 1;
}
/claes