C # datetime formatting Daquan and Usage Summary

Source: Internet
Author: User

//c#datetime formattingDateTime dt =DateTime.Now; Label1.Text= dt. ToString ();//2005-11-5 13:21:25Label2.Text = dt. Tofiletime (). ToString ();//127756416859912816Label3.text = dt. TOFILETIMEUTC (). ToString ();//127756704859912816Label4.text = dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25Label5.text = dt. Tolongdatestring (). ToString ();//November 5, 2005Label6.text = dt. Tolongtimestring (). ToString ();//13:21:25Label7.text = dt. ToOADate (). ToString ();//38661.5565508218Label8.text = dt. ToShortDateString (). ToString ();//2005-11-5Label9.text = dt. Toshorttimestring (). ToString ();//13:21Label10.text = dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25//2005-11-5 13:30:28.4412864Label1.Text = dt. Year.tostring ();//2005Label2.Text = dt. Date.tostring ();//2005-11-5 0:00:00Label3.text = dt. Dayofweek.tostring ();//SaturdayLabel4.text = dt. Dayofyear.tostring ();//309Label5.text = dt. Hour.tostring ();// -Label6.text = Dt.Millisecond.ToString ();//441Label7.text = dt. Minute.tostring ();// -Label8.text = dt. Month.tostring ();// OneLabel9.text = dt. Second.tostring ();// -Label10.text = dt. Ticks.tostring ();//632667942284412864Label11.text = dt. Timeofday.tostring ();//13:30:28.4412864Label1.Text = dt. ToString ();//2005-11-5 13:47:04Label2.Text = dt. AddYears (1). ToString ();//2006-11-5 13:47:04Label3.text = dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04Label4.text = dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04Label5.text = dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04Label6.text = dt. AddMonths (1). ToString ();//2005-12-5 13:47:04Label7.text = dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05Label8.text = dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10Label9.text = dt. Addticks ( +). ToString ();//2005-11-5 13:47:04Label10.text = Dt.compareto (dt). ToString ();//0//label11.text = dt. ADD (?). ToString ();//question mark is a time periodLabel1.Text = dt. Equals ("2005-11-6 16:11:04"). ToString ();//FalseLabel2.Text = dt. Equals (DT). ToString ();//TrueLabel3.text = dt. GetHashCode (). ToString ();//1474088234Label4.text = dt. GetType (). ToString ();//System.DateTimeLabel5.text = dt. GetTypeCode (). ToString ();//DateTimeLabel1.Text= dt. Getdatetimeformats ('s')[0]. ToString ();//2005-11-05t14:06:25Label2.Text = dt. Getdatetimeformats ('T')[0]. ToString ();//14:06Label3.text = dt. Getdatetimeformats ('y')[0]. ToString ();//November 2005Label4.text = dt. Getdatetimeformats ('D')[0]. ToString ();//November 5, 2005Label5.text = dt. Getdatetimeformats ('D')[1]. ToString ();//2005Label6.text = dt. Getdatetimeformats ('D')[2]. ToString ();//Week 62,005Label7.text = dt. Getdatetimeformats ('D')[3]. ToString ();//Saturday November 5, 2005Label8.text = dt. Getdatetimeformats ('M')[0]. ToString ();//November 5Label9.text = dt. Getdatetimeformats ('F')[0]. ToString ();//November 5, 2005 14:06Label10.text = dt. Getdatetimeformats ('g')[0]. ToString ();//2005-11-5 14:06Label11.text = dt. Getdatetimeformats ('R')[0]. ToString ();//Sat, Nov 2005 14:06:25 GMTLabel1.Text=?string. Format ("{0:D}", DT);//2005-11-5Label2.Text =?string. Format ("{0:D}", DT);//November 5, 2005Label3.text =?string. Format ("{0:f}", DT);//November 5, 2005 14:23Label4.text =?string. Format ("{0:f}", DT);//November 5, 2005 14:23:23Label5.text =?string. Format ("{0:g}", DT);//2005-11-5 14:23Label6.text =?string. Format ("{0:g}", DT);//2005-11-5 14:23:23Label7.text =?string. Format ("{0:m}", DT);//November 5Label8.text =?string. Format ("{0:r}", DT);//Sat, Nov 2005 14:23:23 GMTLabel9.text =?string. Format ("{0:s}", DT);//2005-11-05t14:23:23Label10.text =string. Format ("{0:t}", DT);//14:23Label11.text =string. Format ("{0:t}", DT);//14:23:23Label12.text =string. Format ("{0:u}", DT);//2005-11-05 14:23:23zLabel13.text =string. Format ("{0:u}", DT);//November 5, 2005 6:23:23Label14.text =string. Format ("{0:y}", DT);//November 2005Label15.text =string. Format ("{0}", DT);//2005-11-5 14:23:23?Label16.text =string. Format ("{0:YYYYMMDDHHMMSSFFFF}", DT);//Yyyymm can be set, such as Label16.text = string. Format ("{0:YYYYMMDD}", DT);

--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 The current month date, in the format: 2003-9-23
String strymd=currenttime.tostring ("D");
1.13 Take the current time, in the format: 14:24
String strt=currenttime.tostring ("T");
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, knowing that the day of the week is the first 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 here 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 is 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, the same thing, a week is 7 days, last week is this week minus 7 days, and 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, many people will say that the first day of this month is definitely number 1th, and the last day is the next month minus another day. Of course it's right.
The 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

C # datetime formatting Daquan and Usage Summary

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.