Using DateTime will make it easier for you to deal with these things.

Source: Internet
Author: User

In C #, the built-in datetime can basically implement these functions, and using DateTime will make it easier for you to deal with these things.
Today
DateTime.Now.Date.ToShortDateString ();
yesterday , it was today's date minus one.
DateTime.Now.AddDays (-1). ToShortDateString ();
Tomorrow, in the same vein, add a
DateTime.Now.AddDays (1). ToShortDateString ();

this week (to know that the first day of the week is the day of the week and that the first day of the week is a few days ago, it is important to note that every week is 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 do not understand, then look at the Chinese display of the day of the week should understand the method
Since DayOfWeek returns the number of days of the week, we want to convert it into Chinese characters to facilitate our reading, some people may use switch to a control, in fact, 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 this week minus 7 days, next week is the same
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 , a lot of people will say that the first day of this month is definitely number 1th, the last day is the next month, minus another day. Of course, this is the right
general wording
DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1";//First day
DateTime.Parse ( DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"). AddMonths (1). AddDays (-1). ToShortDateString ();//Last day

It 's easier to use ToString's character formatting in C #
DateTime.Now.ToString ("yyyy-mm-01");
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (1). AddDays (-1). ToShortDateString ();

Last month, minus one month
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (-1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();
Next month, add a 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 ();
After 7 days
DateTime.Now.Date.ToShortDateString ();
DateTime.Now.AddDays (7). ToShortDateString ();
7 days ago
DateTime.Now.AddDays (-7). ToShortDateString ();
DateTime.Now.Date.ToShortDateString ();

This year , formatted with ToString characters we can easily figure out the first and last day 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 ();
last year , no more explaining
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (-1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddDays (-1). ToShortDateString ();
Next year
DateTime.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 feel the difficulty here, need to write a long process to judge. In fact, we all know that four quarters a year, three months a quarter
First we push the date to the first month of the quarter, and 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 the 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 .... Call it
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 Supplement ======

How to get time in C #!
1, DateTime Digital type
System.DateTime currenttime=new System.DateTime ();
1.1 Minutes and seconds of the current month and day
Currenttime=system.datetime.now;
1.2 Take the year before
int year =currenttime.year;
1.3 Take Current month
int month =currenttime.month;
1.4 Take the current day
int day =currenttime.day;
1.5 Take current time
int when =currenttime.hour;
1.6 Taking current points
int sub-=currenttime.minute;
1.7 Take current seconds
int seconds =currenttime.second;
1.8 Take the current millisecond
int milliseconds =currenttime.millisecond;
(Variable available in Chinese)

1.9 Chinese date display--month and day
String Stry=currenttime.tostring ("F"); Do not display seconds

1.10 Chinese Date Display _ Month
String strym=currenttime.tostring ("Y");

1.11 Chinese Date Display _ Month Day
String strmd=currenttime.tostring ("M");

1.12 Chinese Month Day
String strymd=currenttime.tostring ("D");

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

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

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

1.16 Take the current time, in the format: 2003-09-23 14:48
String strt=currenttime.tostring ("G");

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

1.18 Date time to get the current time n days
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; Take the current month day and seconds
String y=currenttime.year.tostring (); Take when the year before
String m=currenttime.month.tostring (); Take Current month
String d=currenttime.day.tostring (); Take the current day
String t=currenttime.hour.tostring (); Take current time
String mm=currenttime.minute.tostring (); Take current points
String s=currenttime.second.tostring (); Take current seconds
String ss=currenttime.millisecond.tostring (); Take the current millisecond
String filename=y+m+d+t+mm+s+ss+ ". Html "; After a join, get a long file name

Using DateTime will make it easier for you to deal with these things.

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.