4 Replies - 2859 Views - Last Post: 13 November 2009 - 09:39 AM Rate Topic: -----

#1 ismailc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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

Please Assist!

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  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 djkitt  Icon User is offline

  • D.I.C Head

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

Re: Week Number of day, like VB Function DATEPART

Posted 13 November 2009 - 09:04 AM

So,

I think your problem might result from using a double for your calculation... Try 'ToDecimal' for your conversion instead...

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
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1