# Week Number of day, like VB Function DATEPART

Page 1 of 1

## 4 Replies - 3034 Views - Last Post: 13 November 2009 - 09:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=138776&amp;s=b6df7664230d9da85c106fe3987fe5ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ismailc

Reputation: 3
• Posts: 98
• Joined: 14-September 07

# Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 02:49 AM

Hi, I need help.

I need to get the week number of day,

like the VB Function:
``` DATEPART("ww", Now())
```

I tried this but it does not calculate correctly as when the 1st is on Saterday (week1) & calculates on the 3rd Monday
the below code sees it as still part of week1 & not week 2
```  WeekOfYear = Convert.ToString(Math.Ceiling(Convert.ToDouble(ManufactureDateVar.DayOfYear) / 7));

```

I need to somehow replicate the VB Function in C# to the correct week number

This post has been edited by ismailc: 13 November 2009 - 02:51 AM

Is This A Good Question/Topic? 0

## Replies To: Week Number of day, like VB Function DATEPART

### #2 Crehl

Reputation: 6
• Posts: 49
• Joined: 25-January 09

## Re: Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 04:51 AM

Take note that this reply is most likely not the best method, you should look for a better solution first before resorting to crude tactics like this one.

From the sounds of things, you need to add some sort of bias (Or whatever you'd like to call it) to the day before dividing by 7. In the case of Week 1 starting on saturday (And presuming you regard the week as starting on monday), this means adding 5.

Saturday the 1st: Ceil((1 + 5) / 7) = 1
Sunday the 2nd: Ceil((2 + 5) / 7) = 1
Monday the 3rd: Ceil((3 + 5) / 7) = 2

This bias accounts for all of the days in the first week which were actually part of the previous year (In the above example, the 5 days Mon-Fri were in the previous year). Hence, you can simply define the bias as follows:

First day of year : Bias

Monday : 0
Tuesday : 1
Wednesday : 2
..etc

There may be a proper way of doing this, so I suggest you check for a more 'official' solution, ie using a different method in the api rather than adjusting the input.

Hth,
-Crehl

Reputation: 66
• Posts: 560
• Joined: 27-October 09

## Re: Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 05:05 AM

I am just guessing, and don't have time to check if this actually works, but I think if you just say

(DayOfYear / 7) + 1

that should give you which week throught the year that is. You can also work with DayOfYear as an int in this case too. In order for it to hopefully work DayOfYear has to be an int.

i.e. DayOfYear = 3 3/7=0+1=1(first week of year)
DayOfYear = 8 8/7=1+1=2(second week of year)
and so on...

Hope this was helpful to you. Like I said I haven't tested it or not, but the idea is logical.

### #4 djkitt

Reputation: 29
• Posts: 189
• Joined: 22-May 08

## Re: Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 09:04 AM

So,

```WeekOfYear = Convert.ToString(Math.Ceiling(Convert.ToDecimal(ManufactureDateVar.DayOfYear) / 7));

```

The float and double variables are approximate values and this can sometimes throw off calculations a bit.

Hope this helps,

-Kitt

### #5 Momerath

• D.I.C Lover

Reputation: 1019
• Posts: 2,462
• Joined: 04-October 09

## Re: Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 09:39 AM

If it must behave exactly like the VB one, just add a reference to the Microsoft.VisualBasic dll and use the VB DateAndTime class.