Obtain the first and last days of a month.
/// <Summary> /// obtain the first day of a month // </Summary> /// <Param name = "datetime"> obtain the first day of a month </ param> // <returns> </returns> private datetime firstdayofmonth (datetime) {return datetime. adddays (1-datetime. day );} /** // <summary> // obtain the last day of a month. // </Summary> // <Param name = "datetime"> obtain the month. last day </param> // <returns> </returns> private datetime lastdayofmonth (datetime) {return datetime. adddays (1-datetime. day ). addmonths (1 ). adddays (-1 );} /** // <summary> // obtain the first day of the previous month. // </Summary> // <Param name = "datetime"> obtain the current day of the previous month. time </param> /// <returns> </returns> private datetime firstdayofpreviousmonth (datetime) {return datetime. adddays (1-datetime. day ). addmonths (-1 );} /** // <summary> // obtain the last day of the previous month. // </Summary> // <Param name = "datetime"> obtain the last day of the previous month. current time of the day </param> /// <returns> </returns> private datetime lastdayofprdviusmonth (datetime) {return datetime. adddays (1-datetime. day ). adddays (-1 );}
C # datetime plus 1 day minus one day plus January minus January
// Today datetime. now. date. toshortdatestring (); // yesterday, that is, today's date minus a datetime. now. adddays (-1 ). toshortdatestring (); // tomorrow, similarly, add a datetime. now. adddays (1 ). toshortdatestring (); // This Week (you need to know the day of the week first on the first day of the week, so that you can know that the first day of the week is the day of the day a few days ago, note that every week starts from Sunday to Saturday. now. adddays (convert. todouble (0-convert. toint16 (datetime. now. dayofweek )))). toshortdatestring (); datetime. now. adddays (convert. todouble (6-convert. toint16 (datetim E. now. dayofweek )))). toshortdatestring (); // if you still don't understand it, read the Chinese method to display the day of the week. // because dayofweek returns the number of the day of the week, we want to convert it into Chinese characters so that we can read it. Some people may use switches to compare them one by one, in fact, you don't need to bother string [] day = new string [] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday ", "Saturday"}; Day [convert. toint16 (datetime. now. dayofweek)]; // last week, similarly, a week is 7 days, last week is this week minus 7 days, and next week is the same as datetime. now. adddays (convert. todouble (0-convert. toint16 (datetime. now. dayofweek)-7 ). tosho Rtdatestring (); datetime. now. adddays (convert. todouble (6-convert. toint16 (datetime. now. dayofweek)-7 ). tow.datestring (); // datetime next week. now. adddays (convert. todouble (0-convert. toint16 (datetime. now. dayofweek) + 7 ). toshortdatestring (); datetime. now. adddays (convert. todouble (6-convert. toint16 (datetime. now. dayofweek) + 7 ). toshortdatestring (); // this month, many people will say that the first day of this month must be the first day of the month, and the last day is the second day of the month. Of course this is correct // The general syntax datetime. now. year. tostring () + datetime. now. month. tostring () + "1"; // datetime on the first day. parse (datetime. now. year. tostring () + datetime. now. month. tostring () + "1 "). addmonths (1 ). adddays (-1 ). toshortdatestring (); // The Last Day // use the tostring character in C # To Format datetime more easily. now. tostring ("yyyy-MM-01"); datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (1 ). adddays (-1 ). toshortdatestring (); // last month, minus a month date Time. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (-1 ). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring (); // next month, add a month datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (1 ). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (2 ). adddays (-1 ). toshortdatestring (); // datetime after 7 days. now. date. to1_date String (); datetime. now. adddays (7 ). toshortdatestring (); // datetime seven days ago. now. adddays (-7 ). toshortdatestring (); datetime. now. date. tow.datestring (); // this year, formatted with the tostring character, we can easily calculate the first day of the year and datetime of the last day of the year. parse (datetime. now. tostring ("yyyy-01-01 ")). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-01-01 ")). addyears (1 ). adddays (-1 ). toshortdatestring (); // the previous year. You don't need to explain it again. datetime. parse (datetime. now. Tostring ("yyyy-01-01 ")). addyears (-1 ). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-01-01 ")). adddays (-1 ). toshortdatestring (); // datetime of the next year. parse (datetime. now. tostring ("yyyy-01-01 ")). addyears (1 ). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-01-01 ")). addyears (2 ). adddays (-1 ). toshortdatestring (); // this quarter, many people will think this is difficult and need to write a long process to judge. In fact, we don't need to. We all know that we have four quarters of a year, three months of a quarter. // first, we will push the date to the first month of this quarter, then the first day of this month is the first day of this quarter. now. addmonths (0-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01"); // Similarly, the last day of the quarter is the first day of the next quarter minus a datetime. parse (datetime. now. addmonths (3-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring (); // next quarter, I believe you all know it .... Datetime. now. addmonths (3-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01"); datetime. parse (datetime. now. addmonths (6-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). tow.datestring (); // datetime of the previous quarter. now. addmonths (-3-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01"); datetime. parse (datetime. now. addmonths (0-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();