C # yesterday, today, tomorrow, last week, next week, last month, next month, etc.

Source: Internet
Author: User

// Define variables

Private Static datetime currentdatetime; // current time
Private Static datetime lastdatetime; // Yesterday's time
Private Static datetime nextdatetime; // tomorrow's time

Private Static int currentdayofweek; // today's day of the week
Private Static int lastdayofweek; // last day of the week
Private Static int nextdayofweek; // the day of the week tomorrow

Private Static datetime currentstartweek; // Monday of the week
Private Static datetime currentendweek; // Sunday of this week
Private Static datetime currentstartmonth; // the beginning of this month
Private Static datetime currentendmonth; // the end of the month
Private Static datetime currentstartquarter; // The beginning of this quarter
Private Static datetime currentendquarter; // end of this quarter
Private Static datetime currentstartyear; // early this year
Private Static datetime currentendyear; // end of this year

Private Static datetime laststartweek; // Monday of last week
Private Static datetime lastendweek; // last week and Sunday
Private Static datetime laststartmonth; // The beginning of last month
Private Static datetime lastendmonth; // end of last month
Private Static datetime laststartquarter; // the beginning of the last quarter
Private Static datetime lastendquarter; // The end of the last quarter
Private Static datetime laststartyear; // early last year
Private Static datetime lastendyear; // end of last year

Private Static datetime nextstartweek; // Monday of next week
Private Static datetime nextendweek; // Sunday of next week
Private Static datetime nextstartmonth; // early next month
Private Static datetime nextendmonth; // end of next month
Private Static datetime nextstartquarter; // the beginning of the next quarter
Private Static datetime nextendquarter; // end of next quarter
Private Static datetime nextstartyear; // early next year
Private Static datetime nextendyear; // end of next year

 

// Calculate the variable

Currentdatetime = datetime. Now; // current time
Lastdatetime = datetime. Now. adddays (-1); // last day
Nextdatetime = datetime. Now. adddays (1); // tomorrow Time

Currentdayofweek = convert. toint32 (currentdatetime. dayofweek. tostring ("D"); // the day of the week today
Currentdayofweek = convert. toint32 (lastdatetime. dayofweek. tostring ("D"); // the day of the week of yesterday
Currentdayofweek = convert. toint32 (nextdatetime. dayofweek. tostring ("D"); // the day of the week tomorrow

Currentstartweek = currentdatetime. adddays (1-(currentdayofweek = 0 )? 7: currentdayofweek); // Monday of the week
Currentendweek = currentstartweek. adddays (6); // Sunday of the week
Laststartweek = currentstartweek. adddays (-7); // last week, Monday
Lastendweek = currentendweek. adddays (-7); // last week Sunday
Nextstartweek = currentstartweek. adddays (7); // Monday of next week
Nextendweek = currentendweek. adddays (7); // Sunday of the next week

Currentstartmonth = currentdatetime. adddays (1-currentdatetime. Day); // the beginning of this month
Currentendmonth = currentstartmonth. addmonths (1). adddays (-1); // End of the month
Laststartmonth = currentstartmonth. addmonths (-1); // The beginning of last month
Lastendmonth = currentstartmonth. adddays (-1); // last month
Nextstartmonth = currentendmonth. adddays (1); // early next month
Nextendmonth = nextstartmonth. addmonths (1). adddays (-1); // end of next month

Currentstartquarter = currentdatetime. addmonths (0-(currentdatetime. Month-1) % 3). adddays (1-currentdatetime. Day); // at the beginning of this quarter
Currentendquarter = currentstartquarter. addmonths (3). adddays (-1); // end of this quarter
Laststartquarter = currentstartquarter. addmonths (-3); // the beginning of the last quarter
Lastendquarter = currentstartquarter. adddays (-1); // The end of the last quarter
Nextstartquarter = currentendquarter. adddays (1); // the beginning of the next quarter
Nextendquarter = nextstartquarter. addmonths (3). adddays (-1); // end of the next quarter

// Annual calculation
Currentstartyear = new datetime (currentdatetime. Year, 1, 1); // early this year
Currentendyear = new datetime (currentdatetime. year, 12, 31); // end of this year
Laststartyear = currentstartyear. addyears (-1); // early last year
Lastendyear = currentendyear. addyears (-1); // end of last year
Nextstartyear = currentstartyear. addyears (1); // early next year
Nextendyear = currentendyear. addyears (1); // end of next year

 

/// <Summary>
/// Returns the string function of the day of the week.
/// </Summary>
/// <Param name = "mydatetime"> </param>
/// <Returns> </returns>

Private string getweekdayname (datetime mydatetime)
{
String week = "";
// Obtain the current day of the week
String dt = mydatetime. dayofweek. tostring ();
// Returns Chinese Characters Based on the obtained weekly English Words
Switch (DT)
{
Case "Monday ":
Week = "Monday ";
Break;
Case "Tuesday ":
Week = "Tuesday ";
Break;
Case "Wednesday ":
Week = "Wednesday ";
Break;
Case "Thursday ":
Week = "Thursday ";
Break;
Case "Friday ":
Week = "Friday ";
Break;
Case "Saturday ":
Week = "Saturday ";
Break;
Case "Sunday ":
Week = "Sunday ";
Break;
}
Return Week;
}

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.