473,218 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Work Week

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
5 4999
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike D | last post by:
I need to provide a pulldown with work weeks displayed. Is there any easy way to do this? I would like to go 6 months from the current date and show a week as Oct. 11 - 15. Which is Monday to...
14
by: deko | last post by:
This runs, but does not narrow to current week. suggestions appreciated! SELECT lngEid, dtmApptDate, Subject, Appt_ID FROM qry002 WHERE (dtmApptDate BETWEEN DateAdd("d",-weekday()+2,) And...
9
by: Ray | last post by:
I need to convert the normal calendar to show the week no., the period no. and the financial year. The financial year format is as follows:- Date start: 2 May, 2005 7 days a week, 4 weeks a...
3
by: | last post by:
Hi!! Did Infragistics or some other company else provide a web calendar conrol with the features and look of Microsoft Outlook calendar? I mean that feature that you can choose Day / Work Week /...
8
by: apartain | last post by:
I am working in Access for a (what I thought would be) simple Work Order and Time database. I am just starting a VB class in hopes to become a real developer, not just a pretend one. In this...
5
by: Chris | last post by:
I am trying to output Monday of the current week i.e. if Monday is the 8th I want to display 'Monday 8th' for any date between Monday 8-14th. I would appreciate any help, the code below is...
3
by: Jim | last post by:
Guys I'm rusty. Haven't messed with Access in a while. I'm trying to determine how many workdays (or weekdays) between two dates. I don't think their are any "canned" functions within access -...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.