C # Date-time operations

Source: Internet
Author: User

Availability of current date time:

DateTime Datetime1=datetime.now;

Date-Time operations:

String str1= (datetime1. Addyears (1)). ToString (); Year added

String str1= (datetime1. Addmonths (1)). ToString ()//Plus month

String str1= (datetime1. AddDays (1)). ToString ()//Date Added

String str1= (datetime1. AddHours (1)). ToString ()/+ hour

String str1= (datetime1. AddMinutes (1)). ToString ()//bonus points

String str1= (datetime1. AddSeconds (1)). ToString ()//plus seconds

String str1= (datetime1. Addmilliseconds (1)). ToString ();/+ MS
Long Long1=system.datetime.daysinmonth (2005,2); Return to a certain period of a few days

String str1=datetime1. Dayofweek.tostring ("D"); Converts a week to 0 characters in Sunday

TimeSpan subtime=datetime1. Subtract (datetime2); The interval between datetime1 and DateTime2 is obtained, and the specific time can be separated from the subtime.

Separation of specific time:

Long long1=datetime1. year;//Year of acquisition
Long long1=datetime1. month;//Get Month
Long long1=datetime1. day;//Date of acquisition
Long long1=datetime1. hour;//get an hour
Long long1=datetime1. minute;//Get points
Long long1=datetime1. second;//Get seconds
Long long1=datetime1.millisecond;//gets milliseconds

The following is a list of specific date formatting usages in some asp.net:


============================================
1. Format Date method of binding timing:

2. Formatting date methods for data controls such as Datagrid/datalist:
E.item.cell[0]. Text = Convert.todatetime (e.item.cell[0]. Text). ToShortDateString ();

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

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

("YyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); Support for traditional databases

5. Convert date display format directly with the ToString method:
DateTime.Now.ToString ("Yyyymmddhhmmss");
DateTime.Now.ToString ("Yyyy/mm/dd hh:mm:ss")

6. Show only years
DataBinder.Eval (Container.DataItem, "StartTime", "{0:yyyy-m}")

7. Show all parts of time, including: Date and hour

dataformatstring= "{0:yyyy-mm-dd HH24:mm:ss}" >

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

1.10 Take Chinese Date show _ Years
String strym=currenttime.tostring ("Y");

1.11 Take Chinese date show _ Month Day
String strmd=currenttime.tostring ("M");

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

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

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

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

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

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

1.18 get the current time n days after the date time
DateTime newday = DateTime.Now.AddDays (100);

----

DateTime dt = DateTime.Now;
Label1.Text = dt. ToString ();//2005-11-5 13:21:25
Label2.Text = dt. Tofiletime (). ToString ();//127756416859912816
Label3.text = dt. TOFILETIMEUTC (). ToString ();//127756704859912816
Label4.text = dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
Label5.text = dt. Tolongdatestring (). ToString ();//November 5, 2005
Label6.text = dt. Tolongtimestring (). ToString ();//13:21:25
Label7.text = dt. ToOADate (). ToString ();//38661.5565508218
Label8.text = dt. ToShortDateString (). ToString ();//2005-11-5
Label9.text = dt. Toshorttimestring (). ToString ();//13:21
Label10.text = dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25

Label1.Text = dt. Year.tostring ();//2005
Label2.Text = dt. Date.tostring ()//2005-11-5 0:00:00
Label3.text = dt. Dayofweek.tostring ();//saturday
Label4.text = dt. Dayofyear.tostring ();//309
Label5.text = dt. Hour.tostring ();//13
Label6.text = Dt.Millisecond.ToString ();//441
Label7.text = dt. Minute.tostring ();//30
Label8.text = dt. Month.tostring ();//11
Label9.text = dt. Second.tostring ();//28
Label10.text = dt. Ticks.tostring ();//632667942284412864
Label11.text = dt. Timeofday.tostring ();//13:30:28.4412864

Label1.Text = dt. ToString ();//2005-11-5 13:47:04
Label2.Text = dt. Addyears (1). ToString ();//2006-11-5 13:47:04
Label3.text = dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04
Label4.text = dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04
Label5.text = dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04
Label6.text = dt. Addmonths (1). ToString ();//2005-12-5 13:47:04
Label7.text = dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05
Label8.text = dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10
Label9.text = dt. Addticks (1000). ToString ();//2005-11-5 13:47:04
Label10.text = Dt.compareto (dt). ToString ();//0
Label11.text = dt. ADD (?). ToString ()//question mark is a time period

Label1.Text = dt. Equals ("2005-11-6 16:11:04"). ToString ();//false
Label2.Text = dt. Equals (DT). ToString ();//true
Label3.text = dt. GetHashCode (). ToString ();//1474088234
Label4.text = dt. GetType (). ToString ();//system.datetime
Label5.text = dt. GetTypeCode (). ToString ();//datetime

Label1.Text = dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25
Label2.Text = dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06
Label3.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 05
Label6.text = dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday 2005 11 05
Label7.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 ();//November 5, 2005 14:06
Label10.text = dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06
Label11.text = dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, Nov 14:06:25 GMT

Label1.Text = string. Format ("{0:d}", DT);//2005-11-5
Label2.Text = string. Format ("{0:d}", dt);//November 5, 2005
Label3.text = string. Format ("{0:f}", dt);//November 5, 2005 14:23
Label4.text = string. Format ("{0:f}", dt);//November 5, 2005 14:23:23
Label5.text = string. Format ("{0:g}", DT);//2005-11-5 14:23
Label6.text = string. Format ("{0:g}", DT);//2005-11-5 14:23:23
Label7.text = string. Format ("{0:m}", dt);//November 5
Label8.text = string. Format ("{0:r}", DT);//sat, Nov-14:23:23 GMT
Label9.text = string. Format ("{0:s}", DT);//2005-11-05t14:23:23
Label10.text = string. Format ("{0:t}", DT);//14:23
Label11.text = string. Format ("{0:t}", DT);//14:23:23
Label12.text = string. Format ("{0:u}", DT);//2005-11-05 14:23:23z
Label13.text = string. Format ("{0:u}", dt);//November 5, 2005 6:23:23
Label14.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);//2005-11-5 13:30:28.4412864

C # compares two time sizes
1, compare the time size of the experiment

String st1= "12:13";

String st2= "14:14";

DateTime Dt1=convert.todatetime (ST1);

DateTime Dt2=convert.todatetime (ST2);

DateTime Dt3=datetime.now;

if (Datetime.compare (DT1,DT2) >0)

Msg. text=st1+ ">" +ST2;

Else

Msg. Text=st1+ "<" +ST2;

Msg. text+= "/r/n" +dt1. ToString ();

if (Datetime.compare (DT1,DT3) >0)

Msg. text+= "/r/n" +st1+ ">" +DT3. ToString ();

Else

Msg. text+= "/r/n" +st1+ "<" +DT3. ToString ();


2, calculate the two time difference function, return the absolute value of the time lag:

private String DateDiff (DateTime datetime1,datetime DateTime2)

{

String Datediff=null;

Try

{

TimeSpan ts1=new TimeSpan (datetime1.ticks);

TimeSpan ts2=new TimeSpan (datetime2.ticks);

TimeSpan Ts=ts1. Subtract (TS2). Duration ();

Datediff=ts. Days.tostring () + "Days" +ts. Hours.tostring () + "Hour" +ts. Minutes.tostring () + "minutes" +ts. Seconds.tostring () + "seconds";

}

Catch

{
}

return DateDiff;

}
3, the realization calculates datetime1-36 day =datetime2 function
TimeSpan ts=new TimeSpan (40,0,0,0);

DateTime dt2=datetime.now.subtract (TS);

Msg. Text=datetime.now.tostring () + "-" +ts. Days.tostring () + "Day/r/n"; Msg. Text+=dt2. ToString ();

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.