C # Time Format and Operation Summary

Source: Internet
Author: User
String str1 = (datetime1.addyears (1 )). tostring (); // Add the year string str1 = (datetime1.addmonths (1 )). tostring (); // Add the month string str1 = (datetime1.adddays (1 )). tostring (); // Add date string str1 = (datetime1.addhours (1 )). tostring (); // Add hour string str1 = (datetime1.addminutes (1 )). tostring (); // string str1 = (datetime1.addseconds (1 )). tostring (); // Add seconds string str1 = (datetime1.addmilliseconds (1 )). tostring (); // Add millisecond long long1 = system. datetime . Daysinmonth (); // returns the string str1 = datetime1.dayofweek for several days in a month. tostring ("D"); // converts a day in a week to a numeric character. Sunday is 0 timespan subtime = datetime1.subtract (datetime2); // obtains the interval between datetime1 and datetime2, the specific time can be separated from subtime.

 

Get the current date and time:

Datetime datetime1 = datetime. now;

Calculation of date and time:

 

Separation of specific time:

 

Long long1 = datetime1.year; // obtain the year long long1 = datetime1.month; // obtain the month long long1 = datetime1.day; // obtain the date long long1 = datetime1.hour; // obtain the hour long long1 = datetime1.minute; // obtain the minute long long1 = datetime1.second; // obtain the second long long1 = datetime1.millisecond; // obtain the millisecond

 

The following lists specific date formatting usage in Asp.net:


========================================================== ====
1. Date Format Method for binding:

2. How to format data items such as DataGrid and datalist:
E. Item. Cell [0]. Text = convert. todatetime (E. Item. Cell [0]. Text). to1_datestring ();

3. Convert the date display format with the string type:
String. Format ("yyyy-mm-dd", yourdatetime );

4. Convert the date display format using the convert method:
Convert. todatetime ("2005-8-23"). tostring

("Yymmdd", system. Globalization. datetimeformatinfo. invariantinfo); // supports traditional Chinese databases.

5. Convert the date display format directly using the tostring method:
Datetime. Now. tostring ("yyyymmddhhmmss ");
Datetime. Now. tostring ("yyyy/mm/dd hh: mm: SS ")

6. show only the year and month
Databinder. eval (container. dataitem, "starttime", "{0: yyyy-m }")

7. display all time parts, including: year, month, day, hour, minute, and second

Dataformatstring = "{0: yyyy-mm-dd hh24: mm: SS}">

 

 

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 );

----

Datetime 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, 2005 label6.text = DT. tolongtimestring (). tostring (); // 13: 21: 25label7. TEXT = DT. tooada Te (). tostring (); // 38661.5565508218label8.text = DT. tow.datestring (). tostring (); // 2005-11-5label9.text = DT. tow.timestring (). tostring (); // 13: 21label10. TEXT = DT. touniversaltime (). tostring (); // 2005-11-5 5: 21: 25label1. TEXT = DT. year. tostring (); // 2005label2. TEXT = DT. date. tostring (); // 2005-11-5 0: 00: 00label3. TEXT = DT. dayofweek. tostring (); // saturdaylabel4.text = DT. dayofyear. tostring (); // 309 Labe L5.text = DT. hour. tostring (); // 13label6. TEXT = DT. millisecond. tostring (); // 441label7. TEXT = DT. minute. tostring (); // 30label8. TEXT = DT. month. tostring (); // 11label9. TEXT = DT. second. tostring (); // 28label10. 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.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.1 ). tostring (); // 2005-11-5 13: 47: 05label8. TEXT = DT. addminutes (1.1 ). tostring (); // 2005-11-5 13: 48: 10 label 9. TEXT = DT. addticks (1000 ). tostring (); // 2005-11-5 13: 47: 04label10. TEXT = DT. compareto (DT ). tostring (); // 0label11. TEXT = DT. add (?). Tostring (); // question mark is a time period label1.text = DT. equals ("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 2005 label4.text = DT. getdatetimeformats ('D') [0]. tostring (); // November 5, 2005 label5.text = DT. getdatetimeformats ('D') [1]. tostring (); // 2005 11 05label6. TEXT = DT. getdatetimeformats ('D') [2]. tostring (); // sat 2005 11 05label7. TEXT = DT. getdatetimeformats ('D') [3]. tostring (); // Saturday November 5, 2005 label8.text = DT. getdatetimeformats ('M') [0]. tostring (); // November 5 label9.text = DT. getdatetimeformats ('F') [0]. tostring (); // February 14: 06label10. TEXT = DT. getdatetimeformats ('G') [0]. tostring (); // 2005-11-5 14: 06label11. TEXT = DT. getdatetimeformats ('R') [0]. tostring (); // sat, 05 Nov 2005 14:06:25 gmtlabel1.text = string. format ("{0: d}", DT); // 2005-11-5label2.text = string. format ("{0: d}", DT); // November 5, 2005 label3.text = string. format ("{0: f}", DT); // February 14: 23label4. TEXT = string. format ("{0: f}", DT); // August 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 5 label8.text = string. format ("{0: R}", DT); // sat, 05 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 2005 label15.text = string. format ("{0}", DT); // 2005-11-5 14:23:23 label16.text = string. format ("{0: yyyymmddhhmmssffff}", DT); // 13:30:28. 4412864

 

 

 

C # compare the two time sizes
1. Compare time-based experiments

 1 string st1="12:13"; 2  3 string st2="14:14"; 4  5 DateTime dt1=Convert.ToDateTime(st1); 6  7 DateTime dt2=Convert.ToDateTime(st2); 8  9 DateTime dt3=DateTime.Now;10 11 if(DateTime.Compare(dt1,dt2)>0)12 13 msg.Text=st1+">"+st2;14 15 else16 17 msg.Text=st1+"<"+st2;18 19 msg.Text+="\r\n"+dt1.ToString();20 21 if(DateTime.Compare(dt1,dt3)>0)22 23 msg.Text+="\r\n"+st1+">"+dt3.ToString();24 25 else26 27 msg.Text+="\r\n"+st1+"<"+dt3.ToString();

 

2. Calculate the two time difference functions and return the absolute value of the time difference:

1 private string datediff (datetime datetime1, datetime datetime2) 2 3 {4 5 string datediff = NULL; 6 7 Try 8 9 {10 11 timespan ts1 = new timespan (datetime1.ticks ); 12 13 timespan ts2 = new timespan (datetime2.ticks); 14 15 timespan Ts = ts1.subtract (ts2 ). duration (); 16 17 datediff = ts. days. tostring () + "day" + ts. hours. tostring () + "Hour" + ts. minutes. tostring () + "Minute" + ts. seconds. tostring () + "seconds"; 18 19} 20 21 catch22 23 {24} 25 26 return datediff; 27 28}

 

 

3. realize the function of calculating DateTime1-36 day = datetime2

1 timespan Ts = new timespan (40, 0, 0, 0); 2 3 datetime dt2 = datetime. now. subtract (TS); 4 5 MSG. TEXT = datetime. now. tostring () + "-" + ts. days. tostring () + "day \ r \ n"; MSG. text + = dt2.tostring ();

 

 

Reproduced in: http://VisualStudio.cnblogs.com/

C # Time Format and Operation Summary

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.