Vinit Patel

How do i get week of the month?

Posted on: September 25, 2009


  1. // —- GetWeekOfMonth —————————
  2. //
  3. // Assuming Sunday starts a new week, get the ordinal
  4. // week a date is in…of the date’s month
  5. // using Microsoft.VisualBasic;
  6. int GetWeekOfMonth(DateTime Date)
  7. {
  8. long Week;
  9. // get first day of month
  10. DateTime BaseDate = new DateTime(Date.Year, Date.Month, 1);
  11. // get difference, in weeks, between the date and first day of month
  12. Week = DateAndTime.DateDiff(DateInterval.WeekOfYear,
  13. BaseDate,
  14. Date,
  15. Microsoft.VisualBasic.FirstDayOfWeek.Sunday,
  16. FirstWeekOfYear.Jan1);
  17. // want it one, not zero based
  18. return (int)Week + 1;
  19. }
  20. // test it
  21. protected void Button1_Click(object sender, EventArgs e)
  22. {
  23. DateTime Date = new DateTime(2007, 3, 1);
  24. while (Date.Month == 3)
  25. {
  26. int Week = GetWeekOfMonth(Date);
  27. Response.Write(String.Format(“{0:d} : {1} <BR>”, Date, Week));
  28. Date = Date.AddDays(1);
  29. }
  30. }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Calendar

September 2009
M T W T F S S
« Jul   Oct »
 123456
78910111213
14151617181920
21222324252627
282930  

Stats Of Blog

  • 6,962 hits

Cluster Map

%d bloggers like this: