C # obtain from the current time, this week, this month, and this quarter... Time period

Source: Internet
Author: User
From: http://hi.baidu.com/jzfkj/blog/item/61cadf95a65ab114d31b70fd.html

Datetime dt = datetime. now; // The current time datetime startweek = DT. adddays (1-convert. toint32 (DT. dayofweek. tostring ("D"); // datetime endweek = startweek on Monday of this week. adddays (6); // datetime startmonth = DT on Sunday of this week. adddays (1-DT. day); // datetime endmonth = startmonth at the beginning of this month. addmonths (1 ). adddays (-1); // The End of the month // datetime endmonth = startmonth. adddays (DT. addmonths (1)-Dt ). days-1); // datetime startquarter = DT at the end of the month. addmonths (0-(DT. month-1) % 3 ). adddays (1-DT. day); // datetime endquarter = startquarter at the beginning of this quarter. addmonths (3 ). adddays (-1); // datetime startyear = new datetime (DT. year, 1, 1); // datetime endyear = new datetime (DT. year, 12, 31); // End of the year

 

 

For yesterday, tomorrow, last week, last month, last quarter, and last year, you only need to combine adddays (), addmonths (), and addyears.

Use of datetime in C #
// Such as annual sales, quarterly profit, and 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, 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"; // The first day
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). to1_datestring ();

// Last month, minus a month
Datetime. parse (datetime. Now. tostring ("yyyy-MM-01"). addmonths (-1). to1_datestring ();
Datetime. parse (datetime. Now. tostring ("yyyy-MM-01"). adddays (-1). to1_datestring ();
// Add a month to the next month
Datetime. parse (datetime. Now. tostring ("yyyy-MM-01"). addmonths (1). to1_datestring ();
Datetime. parse (datetime. Now. tostring ("yyyy-MM-01"). addmonths (2). adddays (-1). to1_datestring ();
// 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"). to1_datestring ();
Datetime. parse (datetime. Now. tostring ("yyyy-01-01"). addyears (1). adddays (-1). to1_datestring ();
// For the previous year, do not explain it again
Datetime. parse (datetime. Now. tostring ("yyyy-01-01"). addyears (-1). to1_datestring ();
Datetime. parse (datetime. Now. tostring ("yyyy-01-01"). adddays (-1). to1_datestring ();
// Next year
Datetime. parse (datetime. Now. tostring ("yyyy-01-01"). addyears (1). to1_datestring ();
Datetime. parse (datetime. Now. tostring ("yyyy-01-01"). addyears (2). adddays (-1). to1_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 the quarter, and then the first day of the month will be 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 ();

 

 

Additional article:

/// <Summary>
/// Calculate the start date of the week (the date of the week)
/// </Summary>
/// <Param name = "somedate"> any day of the week </param>
/// <Returns> returns the day of the week. The specific time, minute, and second are equal to the input value. </returns>
Public static datetime calculatefirstdateofweek (datetime somedate)
{
Int I = somedate. dayofweek-dayofweek. Monday;
If (I =-1) I = 6; // I value> = 0. Due to enumeration, Sunday is at the top of the list. At this time, Sunday-Monday =-1, must be + 7 = 6.
Timespan Ts = new timespan (I, 0, 0, 0 );
Return somedate. Subtract (TS );
}

///


// calculate the end date of the week (the day of the week)
///
/// any day of the week
// returns the Sunday date, the following time, minute, and second values are equal to the input values
Public static datetime calculatelastdateofweek (datetime somedate)
{< br> int I = somedate. dayofweek-dayofweek. sunday;
if (I! = 0) I = 7-I; // due to enumeration, Sunday is at the top of the list, and the subtraction interval is reduced by 7.
timespan Ts = new timespan (I, 0, 0, 0);
return somedate. Add (TS);
}

/// <Summary>
/// Determine whether the selected date is this week (this week is determined by the system's current time)
/// </Summary>
/// <Param name = "somedate"> </param>
/// <Returns> </returns>
Public static bool isthisweek (datetime somedate)
{
// Obtain Monday corresponding to somedate
Datetime somemon = calculatefirstdateofweek (somedate );
// Get this Monday
Datetime nowmon = calculatefirstdateofweek (datetime. Now );

Timespan Ts = somemon-nowmon;
If (TS. Days <0)
TS =-ts; // positive
If (TS. days> = 7)
{
Return false;
}
Else
{
Return true;
}
}

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.