Getting week of the month from a DateTime variable |
// ---- GetWeekOfMonth --------------------------- // // Assuming Sunday starts a new week, get the ordinal // week a date is in...of the date's month // using Microsoft.VisualBasic; int GetWeekOfMonth(DateTime Date) { long Week; // get first day of month DateTime BaseDate = new DateTime(Date.Year, Date.Month, 1); // get difference, in weeks, between the date and first day of month Week = DateAndTime.DateDiff(DateInterval.WeekOfYear, BaseDate, Date, Microsoft.VisualBasic.FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1); // want it one, not zero based return (int)Week + 1; } // test it protected void Button1_Click(object sender, EventArgs e) { DateTime Date = new DateTime(2007, 3, 1); while (Date.Month == 3) { int Week = GetWeekOfMonth(Date); Response.Write(String.Format("{0:d} : {1} <BR>", Date, Week)); Date = Date.AddDays(1); } } |
Thursday, July 9, 2009
Getting week of the month from a Date
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment