## 21 replies

Last post Apr 03, 2009 09:02 AM by flyby35

• flyby35

Member

43 Points

390 Posts

### Calculate # for work week per for a year

Hi everyone

I'm doing a timesheet program.  Our work week is not Monday to Fri, it's Thursday to Wed.  I already found a way to find Start week & end week base on any given date.

However, I'm stuck on this issue.  I don't know how to calculate # for of work week per year.  They want me to calculate like this.  Of course that we have 52 week for a year but they want to know what week it is base on any given date.

Base on any given date, They should able to know what work week # it is in a year Start date & cut date.

For example, for date Feb 03-2009, it is work week # 6, start date is 1/29/09, cut date 2/4/2009

Any ideas?

Thanks

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

If you are using SQL Server then use these commands in stored procedure to find out what week number current date belong to:

SET DATEFIRST 4

SELECT DATEPART(wk,@date)

this will return the weeknumber.

Please note these values for setting the DateFirst.

 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 (default, U.S. English) Sunday

I hope this solves your problem.

Avi
• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

I'm not doing from SQL table.

I tried to do from coding. I want to prevent having too much data in my SQL

If a user enter/select a date (anydate), it will automatically calculate Week start date & week end date (work week is from Thurs to Wed).  This, I"m already done

I still can't figureout how to find the Week # base on my custom work week

• jnsuryapraka...

Participant

800 Points

248 Posts

### Re: Calculate # for work week per for a year

I think you can achieve this with below code.....

System.Globalization.Calendar cal = cult_info.Calendar;
int weekNo = cal.GetWeekOfYear(date, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek);
return weekNo;

or Refer to the below rule.

http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear(VS.71).aspx

Thanks,
Surya Prakash Jayanti
http://suryaprakashj.blogspot.com
http://jnsuryaprakash.blogspot.com

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

Refer this link. It might help out in figuring the logic.

http://www.merlyn.demon.co.uk/weekcalc.htm

Avi
• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

can you show me how to apply your codes?  I'd like to display the week number into a label control

Thanks

• infosolvex

Member

621 Points

129 Posts

### Re: Calculate # for work week per for a year

The following code will give you the week of the year based on the date.

private int getWeek(DateTime dt)
{

int year = dt.Year;
int mth = dt.Month;
int days =0;
int i=1;

while(i<=mth)
{
days = days + DateTime.DaysInMonth(dt.Year, mth);
i++;
}

return days/7;

}

Ritesh Ramesh

• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

My work week is not from Mon to Fri.  my work week is from Thurs to Wed, does your codes work with this custom work week?

Thanks

• infosolvex

Member

621 Points

129 Posts

### Re: Calculate # for work week per for a year

That shouldnt be a problem just ad a condition, just check the syntax

if(dt.Day>"Wed") - this is just the pseudo code you'll need to work the correct syntax here

return days/7;

else

return days/7-1;

Let me know if this fixed your problem!

Ritesh Ramesh

• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

I still couldn't figure it out.

This is the code that I have for calculate Start Date & end date base on my Thur - Wed work week

void GetWorkWeekDates(DateTime Input, out DateTime Start, out DateTime End)

{

while (Input.Date.DayOfWeek != DayOfWeek.Thursday)

Start = Input;

}

protected void testbtn_Click(object sender, EventArgs e)

{

DateTime Test = Convert.ToDateTime(TextBox1.Text);

DateTime Start, End;

GetWorkWeekDates(Test, out Start, out End);

testlbl.Text = Start.ToShortDateString();

}

• jnsuryapraka...

Participant

800 Points

248 Posts

### Re: Calculate # for work week per for a year

I think there are system functions we can make use of , instead of writing our own functions. refer to my post in the above in this chain......

Thanks,
Surya Prakash Jayanti
http://suryaprakashj.blogspot.com
http://jnsuryaprakash.blogspot.com

• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

I'm still a newbie to C#.  I don't know how to apply your codes, can you help?

Thank you so much

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

try out this function. It returns the Week Number and you have to pass the date.

protected Int32 GetWeekNumber(DateTime Input)

{

DateTime YearStartDate = new DateTime(Input.Year, 1, 1);while (YearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

int Days = Input.Subtract(YearStartDate).Days;

while (Days < 0)

{

DateTime PreviousYearStartDate = new DateTime(Input.Year - 1, 1, 1);while (PreviousYearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

Days = Input.Subtract(PreviousYearStartDate).Days;

}

int week = Convert.ToInt32(Days / 7);return (week + 1);

}

Please test it fully before using it.

Avi
• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

sorry for asking again, how do I use your function? I mean how do I pass in the date?

I have this button click event

protected void testbtn_Click(object sender, EventArgs e)

{

DateTime Test = Convert.ToDateTime(TextBox1.Text);

GetWeekNumber(Test);

weeknumber.Text =  ..... ???

}

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

its like this:

DateTime Test = Convert.ToDateTime(TextBox1.Text);

weeknumber.Text =  GetWeekNumber(Test).ToString();

Let us know the results.

Avi
• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

It's kind of strange.  I have an old MS Access file from the old programmer (no longer work here), I don't know how he calculate but his result is not the same with the output of your codes

for 2009, week 1, it's from  12/25/2008  -- 12/31/2008 (our work week is Thur to Wed)

I don't know how he calculated it

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

In the logic i'm setting the Year's First Thursday to be Week 1.

Since you want 12/25/2008 to be week 1. Fix that Date as Parameter to the function and set it to be the YearStartDate.

Then calculate the week number based on the user input date.

Avi
• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

Thank you very much, your codes work great

• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

I have a big problem with the codes.

Your codes can calculate the week number base on the 1st Thursday of the year which is fine.  However, Payroll said that they have to have 52 week of the year.  With your codes, if I enter 12/31/2009, it shows 53 week, which is wrong.  It suppose to be week 1 of 2010 (for Dec 31, 09)

Can you fix this?

• flyby35

Member

43 Points

390 Posts

### Re: Calculate # for work week per for a year

#### vemulapalli26

try out this function. It returns the Week Number and you have to pass the date.

protected Int32 GetWeekNumber(DateTime Input)

{

DateTime YearStartDate = new DateTime(Input.Year, 1, 1);while (YearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

int Days = Input.Subtract(YearStartDate).Days;

while (Days < 0)

{

DateTime PreviousYearStartDate = new DateTime(Input.Year - 1, 1, 1);while (PreviousYearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

Days = Input.Subtract(PreviousYearStartDate).Days;

}

int week = Convert.ToInt32(Days / 7);return (week + 1);

}

Please test it fully before using it.

The above codes from you work fine for 2008.  However Accounting only want 52 pay week for a year, so if I enter 12-31-2009, it shows week of 53rd.  Is there anyway that you can fix this?

So each year, there are certain day remainder of the year that will get push off to the next year because they only want 52 week

Thanks

• vemulapalli2...

Member

420 Points

114 Posts

### Re: Calculate # for work week per for a year

here is the logic. test it out.

protected Int32 GetWeekNumber(DateTime Input)

{

int week = 0;

DateTime YearStartDate = new DateTime(Input.Year, 1, 1);

if (YearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

{

week = 1;

}

while (YearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

int Days = Input.Subtract(YearStartDate).Days;while (Days < 0)

{

DateTime PreviousYearStartDate = new DateTime(Input.Year - 1, 1, 1);

while (PreviousYearStartDate.Date.DayOfWeek != DayOfWeek.Thursday)

Days = Input.Subtract(PreviousYearStartDate).Days;

}

week = week +
Convert.ToInt32(Days / 7);if (week >= 52)

{

week = 0;

}

return (week + 1);

}

Avi
• flyby35

Member

43 Points

390 Posts