Summary of using skills of DateTime in C

  • 2021-09-24 23:21:09
  • OfStack

//C # Get time periods such as this week, next week, this month, next month and this quarter according to the current time
DateTime dt = DateTime. Now; //Current time

DateTime startWeek = dt. AddDays (1-Convert. ToInt32 (dt. DayOfWeek. ToString ("d")); //This week on Monday
DateTime endWeek = startWeek. AddDays (6); //This Sunday
DateTime startMonth = dt. AddDays (1-dt. Day); //At the beginning of this month
DateTime endMonth = startMonth. AddMonths (1). AddDays (-1); //End of this month

DateTime startQuarter = dt. AddMonths (0-dt. Month-1)% 3). AddDays (1-dt. Day); //At the beginning of this quarter
DateTime endQuarter = startQuarter. AddMonths (3). AddDays (-1); //End of this quarter

DateTime startYear = new DateTime (dt. Year, 1, 1); //At the beginning of this year
DateTime endYear = new DateTime (dt. Year, 12, 31); //End of this year

//As for yesterday, tomorrow, last week, last month, last quarter, last year, etc., just combine AddDays (), AddMonths () and AddYears ().

//If you still don't understand, look at the method of displaying the day of the week in Chinese again and you should understand it
//Since DayOfWeek returns the number of days of the week, we need to convert it into Chinese characters for our reading. Some people may use switch to compare them one by one, but it doesn't need to be so troublesome.

string [] Day = new string [] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString();

//Last week, in the same way, one week is seven days, last week is this week minus seven days, and next week is also one
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek))-7); //Last week 1
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek))-7). AddDays (6); //Last weekend (Sunday)
//Next week
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek)) + 7); //Next Monday
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek) + 7). AddDays (6); //Next weekend
//This month, many people will say that the first day of this month must be the 1st, and the last day is the 1st of next month, minus one day. Of course that's right
//1-like writing
DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; //Day 1
DateTime. Parse (DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"). AddMonths (1). AddDays (-1). ToShortDateString (); //Last day

//It is easier to format ToString characters in C #
DateTime.Now.ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();

//Last month, minus one month
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Next month, plus one 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();
//After 7 days
DateTime.Now.Date.ToShortDateString();
DateTime.Now.AddDays(7).ToShortDateString();
//7 days ago
DateTime.Now.AddDays(-7).ToShortDateString();
DateTime.Now.Date.ToShortDateString();

//This year, we can also easily calculate the first and last days of this year with ToString character formatting
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
//Last year, don't explain it any more
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();
//Next year
DateTime.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 find it difficult and need to write a long process to judge. Actually, we all know that there are four quarters in one year and three months in one quarter
//First, let's push the date to the first month of this quarter, and then the first day of this month is the first day of this quarter
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);
//Similarly, the last day of this quarter is the first day of the next quarter minus 1
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. . . . Call it a day
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).ToShortDateString();
//Last quarter
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)). AddDays(1 - DateTime.Now);
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day).AddDays(-1).ToShortDateString();


Related articles: