C # current date and time

Source: Internet
Author: User

// Today

Datetime. Now. Date. tow.datestring ();

// Yesterday, that is, one minus today's date

Datetime. Now. adddays (-1). tow.datestring ();

// Tomorrow, similarly, add one

Datetime. Now. adddays (1). tow.datestring ();

// This Week (to know the first day of this week, you must first know the day of the week, so that the first day of this week is the day of the day a few days ago, note that every week starts from Sunday to Saturday.

Datetime. 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, you should understand how to display the day of the week in Chinese.

// Because dayofweek returns the day of the week of the number, we need to convert it into Chinese characters to facilitate reading. Some people may use the switch to compare them one by one. In fact, this is not so troublesome.

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. The same is true for next week.

Datetime. 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 ();

// Next week

Datetime. 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

// General statement

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

// Easy to format with the tostring character in C #

Datetime. now. tostring ("yyyy-MM-01"); datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (1 ). adddays (-1 ). toshortdatestring ();

// Last month, minus a month

Datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). addmonths (-1 ). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();

// Add a month to the next 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 ();

// 7 days later

Datetime. Now. Date. tow.datestring (); datetime. Now. adddays (7). tow.datestring ();

// Seven days ago

Datetime. Now. adddays (-7). tow.datestring (); datetime. Now. Date. tow.datestring ();

// During the current year, it is easy for us to format with tostring characters to calculate the first and last days of the year.

Datetime. parse (datetime. now. tostring ("yyyy-01-01 ")). toshortdatestring (); datetime. parse (datetime. now. tostring ("yyyy-01-01 ")). addyears (1 ). adddays (-1 ). toshortdatestring ();

// For the previous year, do not 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 find it 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 the month is the first day of the quarter.

Datetime. Now. addmonths (0-(datetime. Now. Month-1) % 3). tostring ("yyyy-MM-01 ");

// Similarly, the last day of this quarter is the first day of the next quarter minus one

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 .... Close

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 )). tostring ("yyyy-MM-01"); datetime. parse (datetime. now. addmonths (0-(datetime. now. month-1) % 3 )). 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.