The code for getting the first day and last day of the month is as follows:
| The code is as follows: |
Copy code |
DateTime now = DateTime. Now; DateTime dt1 = new DateTime (now. Year, now. Month, 1 ); DateTime dt2 = d1.AddMonths (1). AddDays (-1 ); |
Dt1 is the first day of this month, and dt2 is the last day of this month,
The algorithm for the last day is: get the first day of the month, then add January, and then subtract one day.
Example
| The code is as follows: |
Copy code |
DateTime dt = DateTime. Now; // The first day of the month DateTime dt_First = dt. AddDays (-(dt. Day) + 1 ); Label1.Text = dt_First.ToString ("yyyy-MM-dd "); // Add the number of months in the current month to 1 DateTime dt2 = dt. AddMonths (1 ); // Last day of the month DateTime dt_Last = dt2.AddDays (-(dt. Day )); Label2.Text = dt_Last.ToString ("yyyy-MM-dd "); Int weeknow = Convert. ToInt32 (System. DateTime. Now. DayOfWeek ); Int daydiff = (-1) * weeknow + 1; Int dayadd = 7-weeknow; |
We all need to write it by hand. The following code can be used for intelligent judgment.
In this example, you can intelligently determine the number of days each month. You do not need to calculate the number of days. This function will be completed for you:
| The code is as follows: |
Copy code |
Using System; Namespace DotNet. Utilities { Public class DateFormat { // Return the first and last days of each month. Public static void ReturnDateFormat (int month, out string firstDay, out string lastDay) { Int year = DateTime. Now. Year + month/12; If (month! = 12) { Month = month % 12; } Switch (month) { Case 1: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 "); Break; Case 2: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); If (DateTime. IsLeapYear (DateTime. Now. Year )) LastDay = DateTime. Now. ToString (year + "-0" + month + "-29 "); Else LastDay = DateTime. Now. ToString (year + "-0" + month + "-28 "); Break; Case 3: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString ("yyyy-0" + month + "-31 "); Break; Case 4: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 "); Break; Case 5: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 "); Break; Case 6: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 "); Break; Case 7: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 "); Break; Case 8: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 "); Break; Case 9: FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 "); Break; Case 10: FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-" + month + "-31 "); Break; Case 11: FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-" + month + "-30 "); Break; Default: FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 "); LastDay = DateTime. Now. ToString (year + "-" + month + "-31 "); Break; } } } } |