1 /// <summary>
2 // count the number of weeks in a period of time
3 /// </summary>
4 /// <param name = "AStart"> Start date </param>
5 /// <param name = "AEnd"> end date </param>
6 /// <param name = "AWeek"> day of the week </param>
7 /// <returns> Number of returned results </returns>
8 public int TotalWeeks (DateTime AStart, DateTime AEnd, DayOfWeek AWeek)
9 {
10 TimeSpan vTimeSpan = new TimeSpan (AEnd. Ticks-AStart. Ticks );
11 int Result = (int) vTimeSpan. TotalDays/7;
12 for (int I = 0; I <= vTimeSpan. TotalDays % 7; I ++)
13 if (AStart. AddDays (I). DayOfWeek = AWeek)
14 return Result + 1;
15 return Result;
16}/* TotalWeeks */
17
18 private void button#click (object sender, EventArgs e)
19 {
20 Console. WriteLine (TotalWeeks (new DateTime (2007, 06, 01 ),
21 new DateTime (2007, 06, 30), DayOfWeek. Monday ));
22}
Obtain the first and last days of a month.
C #
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
DateTime firstDayOfThisMonth = new DateTime(year, month, 1);
DateTime lastDayOfThisMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));
It takes weeks for SQL to obtain the current month.
Select DATEPART (WEEK, DATEADD (MONTH, 1, GETDATE ()-DAY (GETDATE () + 1)-1) // the WEEK of the last DAY of the current MONTH
Select DATEPART (WEEK, GETDATE ()-DAY (GETDATE () + 1) // the WEEK of the first DAY of the current month
Select DATEPART (WEEK, DAY (DATEADD (MONTH, 1, GETDATE ()-DAY (GETDATE () + 1)-1-(GETDATE ()-DAY (GETDATE ()) + 1) // The total number of weeks in the current month
C # How many weeks are there in the current month?
Int year = DateTime. Now. Year;
Int month = DateTime. Now. Month;
DateTime firstDay = new DateTime (year, month, 1 );
DateTime lastDay = new DateTime (year, month, DateTime. DaysInMonth (year, month ));
// 1. Find the last day of the first week (first obtain the day of the week in February, so that the number of weekends in the first week is known)
Int firstWeekend = firstDay. DayOfYear + (7-Convert.ToInt32 (firstDay. DayOfWeek ));
// 2. Obtain the day of the year.
Int currentDay = lastDay. DayOfYear;
// 3. (The Last Day minus the day of the weekend of the first week)/7 equals to the number of weeks in the first week plus the number of weeks in the first week.
// The only special case that has been taken into consideration is that today is just in the first week, so from the first week is 0 plus the first week of 1 is still 1
Response. Write (Convert. ToInt32 (Math. Ceiling (currentDay-firstWeekend)/7.0) + 1 );
Loading editor...