C # obtain the time period

Source: Internet
Author: User

Two parts of this article:

What is the quarter? Hl3292

Push to end of quarter/3

Datetime. Now. addmonths(0-(datetime. Now. Month-1) % 3 + 3)/3

Part 1:

Datetime today = DT. date; // 00:00:00 today
Datetime Tomorrow = DT. Date. adddays (1); // tomorrow 00:00:00

Datetime startweek = DT. adddays (1-convert. toint32 (Dt. dayofweek. tostring ("D"). date; // Monday 00:00:00

Datetime startnextweek = startweek. adddays (7). date; // Monday 00:00:00 next week

Datetime startmonth = new datetime (Dt. Year, DT. month, 1); // 00:00:00 at the beginning of this month

Datetime startnextmonth = new datetime (Dt. Year, DT. Month + 1, 1); // 00:00:00 at the beginning of next month

Datetime startyear = new datetime (Dt. Year, 1, 1); // 00:00:00 at the beginning of this year

Datetime startnextyear = new datetime (Dt. Year + 1, 1, 1); // 00:00:00 at the beginning of the year

Part 2

-Reprinted http://hi.baidu.com/jhfcjl/blog/item/be101e91d919f687a977a453.html

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 ();
YesterdayThis is the date minus one today.
Datetime. Now. adddays (-1). tow.datestring ();
Tomorrow, similarly, add one
Datetime. Now. adddays (1). tow.datestring ();

This week(To know the day of the week, you must first know the day of the week, so 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.
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, 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 weekSimilarly, one week is 7 days, and the last week is the week minus 7 days. The same is true for the 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 monthMany people will say that the first day of the 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

Use the tostring character in C # To FormatEasier
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 ();
Next month, add a 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 ();
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 ();

This yearFormat with tostring characters. We can easily 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 ();
Last yearNo need to explain it.
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 think that this is difficult and requires 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 ). 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 ();

======= Simple addition ======

How to get the time in C!
1. datetime numeric type
System. datetime currenttime = new system. datetime ();
1.1 take the current year, month, day, hour, minute, second
Currenttime = system. datetime. now;
1.2 get current year
Int year = currenttime. Year;
1.3 take the current month
Int month = currenttime. month;
1.4 get the current day
Int day = currenttime. Day;
1.5 current time
Int = currenttime. hour;
1.6 get the current score
Int = currenttime. minute;
1.7 takes the current second
Int second = currenttime. Second;
1.8 takes the current millisecond
Int millisecond = currenttime. millisecond;
(The variable can be in Chinese)

1.9 display Chinese date -- year, month, and day
String stry = currenttime. tostring ("F"); // seconds not displayed

1.10 use Chinese date to display _ year and month
String strym = currenttime. tostring ("Y ");

1.11 retrieve Chinese Date display _ month/day
String strmd = currenttime. tostring ("M ");

1.12 Chinese year, month, and day
String strymd = currenttime. tostring ("D ");

1.13 get the current time, format: 14: 24
String strt = currenttime. tostring ("T ");

1.14 obtain the current time in the format of 2003-09-23t14: 46: 48
String strt = currenttime. tostring ("S ");

1.15 obtain the current time in the format of 2003-09-23 14: 48: 30z
String strt = currenttime. tostring ("U ");

1.16 The current time. format:
String strt = currenttime. tostring ("G ");

1.17 take the current time, format: Tue, 23 Sep 2003 14:52:40 GMT
String strt = currenttime. tostring ("R ");

1.18 obtain the date and time of the current time n days later
Datetime newday = datetime. Now. adddays (100 );

String strt = datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS ");

System. datetime currenttime = new system. datetime ();
Currenttime = system. datetime. Now; // returns the current year, month, day, hour, minute, and second.
String y = currenttime. year. tostring (); // the current year.
String M = currenttime. Month. tostring (); // obtain the current month
String d = currenttime. Day. tostring (); // the current day.
String T = currenttime. Hour. tostring (); // obtain the current time
String Mm = currenttime. Minute. tostring (); // obtain the current score
String S = currenttime. Second. tostring (); // obtain the current second
String Ss = currenttime. millisecond. tostring (); // obtain the current millisecond
String filename = Y + M + D + T + mm + S + SS + ". html"; // after joining, get the long file name

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.