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 PatelPosted Nov 13, 2009, 6:26 AM
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);
}
ic icPosted Nov 13, 2009, 8:07 AM
ManufactureDateVar = Convert.ToDateTime(lblManufactureDate.Text);WeekOfYear = Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(ManufactureDateVar, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
A Huge Thank You
Nilanka DharmadasaPosted Nov 13, 2009, 6:34 AM
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.
If this answer is useful for u, please mark this answer as accepted.