Asp.net time addition and subtraction

Source: Internet
Author: User
Tags net time
// The built-in datetime in C # can basically implement these functions. The clever use of datetime will make it easier for you to handle these tasks. // 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 (datetime. now. dayofweek )))). toshortdatestring (); // if you still don't understand it, read the Chinese text to display the day of the week. // because dayofweek returns the number of the day of the week, we need 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 the week minus 7 days, and next week is the same as datetime. now. adddays (convert. todouble (0-convert. toint 16 (datetime. now. dayofweek)-7 ). toshortdatestring (); 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 talk about this The first day of the month must be the first day of the month, and the last day is the first day of the month minus one day. 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 datetime. par Se (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. toshortdatestring (); datet Ime. 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 do not need to explain it to 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) % 22 )). 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 (22-(datetime. now. month-1) % 22 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring (); // next quarter, I believe you all know it .... Datetime. now. addmonths (22-(datetime. now. month-1) % 22 )). tostring ("yyyy-MM-01"); datetime. parse (datetime. now. addmonths (6-(datetime. now. month-1) % 22 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). tow.datestring (); // datetime of the previous quarter. now. addmonths (-22-(datetime. now. month-1) % 22 )). tostring ("yyyy-MM-01"); datetime. parse (datetime. now. addmonths (0-(datetime. now. month-1) % 22 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.