473,385 Members | 1,869 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,385 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 5010
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 Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
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 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
Jun 27 '08 #5
Claes Bergefall wrote:
"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.

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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.