[C #] built-in datetime usage

Source: Internet
Author: User
// When you make reports or queries, You can preset optional date ranges (for example) for users)
// Such as annual sales, quarterly profit, new customers this month
// The built-in datetime in C # can basically implement these functions. The clever use of datetime will make it much easier for you to handle these tasks.

// Today
Datetime. Now. Date. tow.datestring ();
// Yesterday is the date of today minus one
Datetime. Now. adddays ( - 1 ). Tow.datestring ();
// Tomorrow, similarly, add one
Datetime. Now. adddays ( 1 ). Tow.datestring ();

// This week (the first day of this week is the day of the week, so that the first day of this week is 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). tow.datestring ();
Datetime. Now. adddays (convert. todouble (( 6 - Convert. toint16 (datetime. Now. dayofweek). tow.datestring ();
// If you still don't understand, you should understand how to display Chinese characters in the day of the week.
// Because dayofweek returns the day of the week of the number, we need to convert it into Chinese characters for us to read. 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 ). Tow.datestring ();
Datetime. Now. adddays (convert. todouble (( 6 - Convert. toint16 (datetime. Now. dayofweek ))) - 7 ). Tow.datestring ();
// Next week
Datetime. Now. adddays (convert. todouble (( 0 - Convert. toint16 (datetime. Now. dayofweek ))) + 7 ). Tow.datestring ();
Datetime. Now. adddays (convert. todouble (( 6 - Convert. toint16 (datetime. Now. dayofweek ))) + 7 ). Tow.datestring ();
// 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 ). Tow.datestring (); // 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 ). Tow.datestring ();

// Last month, minus a month
Datetime. parse (datetime. Now. tostring ( " Yyyy-MM-01 " ). Addmonths ( - 1 ). Tow.datestring ();
Datetime. parse (datetime. Now. tostring ( " Yyyy-MM-01 " ). Adddays ( - 1 ). Tow.datestring ();
// Next month, add a month
Datetime. parse (datetime. Now. tostring ( " Yyyy-MM-01 " ). Addmonths ( 1 ). Tow.datestring ();
Datetime. parse (datetime. Now. tostring ( " Yyyy-MM-01 " ). Addmonths ( 2 ). Adddays ( - 1 ). Tow.datestring ();
// Seven 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, we can easily format it with tostring characters and calculate the first and last days of the year.
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Tow.datestring ();
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Addyears ( 1 ). Adddays ( - 1 ). Tow.datestring ();
// Last year, no need to explain it.
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Addyears ( - 1 ). Tow.datestring ();
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Adddays ( - 1 ). Tow.datestring ();
// Next year
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Addyears ( 1 ). Tow.datestring ();
Datetime. parse (datetime. Now. tostring ( " Yyyy-01-01 " ). Addyears ( 2 ). Adddays ( - 1 ). Tow.datestring ();

// 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 a year, four quarters, one quarter, three months.
// First, we will push the date to the first month of this quarter, and then the first day of this month will be the first day of this 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 ). Tow.datestring ();
// 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 ). Tow.datestring ();
// 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 ). Tow.datestring ();
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.