By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Work Week

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
--
-----------------------
Beste Grsse / Best regards / Votre bien devoue
Kerem Gmrkc
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"CKKwan" <ck****@my-deja.comschrieb im Newsbeitrag
news:7a**********************************@u12g2000 prd.googlegroups.com...
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
Jun 27 '08 #2

P: n/a
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
Jun 27 '08 #3

P: n/a
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
Jun 27 '08 #4

P: n/a
"Arne Vajhj" <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
Jun 27 '08 #5

P: n/a
Claes Bergefall wrote:
"Arne Vajhj" <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.

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;
}
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
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.