Work Week

  • Thread starter Thread starter CKKwan
  • Start date Start date
C

CKKwan

Dear All,

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.

Thanks In Advance
 
Hi CKKWan,

check this out,...

System.Globalization.GregorianCalendar gc = new
System.Globalization.GregorianCalendar();
int nWeek = gc.GetWeekOfYear(DateTime.Now,
System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
MessageBox.Show(nWeek.ToString());

Regards

Kerem


--
 
CKKwan laid this down on his screen :
Dear All,

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.

Thanks In Advance

//using System.Globalization;

CultureInfo local = new CultureInfo("nl-NL");

Calendar cal = local.Calendar;
int week = cal.GetWeekOfYear(DateTime.Now,
local.DateTimeFormat.CalendarWeekRule,
local.DateTimeFormat.FirstDayOfWeek));

Note: adjust the CultureInfo to your wishes.

Hans Kesting
 
CKKwan said:
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
 
Arne Vajhøj said:
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.AddDays(7),
dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
if (week == 53 && nextWeek == 2)
{
week = 1;
}


/claes
 
Claes said:
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.AddDays(7),
dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
if (week == 53 && nextWeek == 2)
{
week = 1;
}

It was the bug I was refeering to, but for workaround I tend
to prefer to DIY.

public static int WeekNumber(int year, int mon, int day)
{
int a = (14 - mon) / 12;
int y = year + 4800 - a;
int m = mon + 12*a - 3;
int JD = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 +
y/400 - 32045;
int d4 = (((JD + 31741 - JD % 7) % 146097) % 36524) % 1461;
int L = d4 / 1460;
int d1 = ((d4 - L) % 365) + L;
return d1 / 7 + 1;
}

Arne
 
Back
Top