Hi, I need help.
I need to get the week number of day,
like the VB Function: [code] DATEPART("ww", Now()) [/code]
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
[code]
WeekOfYear = Convert.ToString(Math.Ceiling(Convert.ToDouble(ManufactureDateVar.DayOfYear) / 7));
[/code]
I need to somehow replicate the VB Function in C# to the correct week number
Please Assist!
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Nov 13, 2009, 1:26 AM EST
Here is What you need.
please check "Do you like this answer check box " if my answer helps you :)
using System.Threading;
private void button1_Click(object sender, EventArgs e)
{
int WeekoftheYear =Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now,System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Saturday);
MessageBox.Show("Week of the Year is " + WeekoftheYear);
}
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 13, 2009, 1:34 AM EST
This is somethign written by me.
DateTime c = DateTime.Now;
int x = (int)Math.Ceiling(Convert.ToDouble(c.DayOfYear) / 7);
if ((c.AddDays(-c.DayOfYear + 1)).DayOfWeek.GetHashCode() <= c.DayOfWeek.GetHashCode())
{
return x;
}
else
{
return ( x + 1);
}
And this is something I found on the web.
using System; using System.Globalization; public class SamplesCalendar { public static void Main() { // Gets the Calendar instance associated with a CultureInfo. CultureInfo myCI = new CultureInfo("en-US"); Calendar myCal = myCI.Calendar; // Gets the DTFI properties required by GetWeekOfYear. CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule; DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek; // Displays the number of the current week relative to the beginning of the year. Console.WriteLine( "The CalendarWeekRule used for the en-US culture is {0}.", myCWR ); Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW ); Console.WriteLine( "Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW )); // Displays the total number of weeks in the current year. DateTime LastDay = new System.DateTime( DateTime.Now.Year, 12, 31 ); Console.WriteLine( "There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear( LastDay, myCWR, myFirstDOW ), LastDay.Year ); } } http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear(VS.71).aspxic ic80 reputationPosted Nov 13, 2009, 3:07 AM EST
ManufactureDateVar = Convert.ToDateTime(lblManufactureDate.Text);WeekOfYear = Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(ManufactureDateVar, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
A Huge Thank You