C # Date formatting

Source: Internet
Author: User

Date conversion

DateTime d t= DateTime.Now;

Dt. ToString ();

' 2005-11-5 13:21:25

Dt. Tofiletime (). ToString ();

' 127756416859912000

Dt. TOFILETIMEUTC (). ToString ();

127756704859912816

Dt. ToLocalTime (). ToString ();

' 2005-11-5 21:21:25

Dt. Tolongdatestring (). ToString ();

' 2005-11-5

Dt. Tolongtimestring (). ToString ();

' 13:21:25

Dt. ToOADate (). ToString ();

' 38661.5565508218

Dt. ToShortDateString (). ToString ();

' 2005-11-5

Dt. Toshorttimestring (). ToString ();

' 13:21

Dt. ToUniversalTime (). ToString ();

' 2005-11-5 5:21:25

Dt. Year.tostring ();

' 2005

Dt. Date.tostring ();

' 2005-11-5 0:00:00

Dt. Dayofweek.tostring ();

' Saturday

Dt. Dayofyear.tostring ();

' 309

Dt. Hour.tostring ();

' 13

Dt.Millisecond.ToString ();

' 441

Dt. Minute.tostring ();

' 30

Dt. Month.tostring ();

' 11

Dt. Second.tostring ();

' 28

Dt. Ticks.tostring ();

' 632667942284412864

Dt. Timeofday.tostring ();

' 13:30:28.4412864

Dt. ToString ();

' 2005-11-5 13:47:04

Dt. AddYears (1). ToString ();

' 2006-11-5 13:47:04

Dt. AddDays (1.1). ToString ();

' 2005-11-6 16:11:04

Dt. AddHours (1.1). ToString ();

' 2005-11-5 14:53:04

Dt. Addmilliseconds (1.1). ToString ();

' 2005-11-5 13:47:04

Dt. AddMonths (1). ToString ();

' 2005-12-5 13:47:04

Dt. AddSeconds (1.1). ToString ();

' 2005-11-5 13:47:05

Dt. AddMinutes (1.1). ToString ();

' 2005-11-5 13:48:10

Dt. Addticks (1000). ToString ();

' 2005-11-5 13:47:04

Dt.compareto (DT). ToString ();

' 0

Dt. ADD (?). ToString ();

' Question mark is a time period

Dt. Equals ("2005-11-6 16:11:04"). ToString ();

' False

Dt. Equals (DT). ToString ();

' True

Dt. GetHashCode (). ToString ();

' 1474088234

Dt. GetType (). ToString ();

' System.DateTime

Dt. GetTypeCode (). ToString ();

' DateTime

Dt. Getdatetimeformats (' s ') [0]. ToString ();

' 2005-11-05t14:06:25

Dt. Getdatetimeformats (' t ') [0]. ToString ();

' 14:06

Dt. Getdatetimeformats (' y ') [0]. ToString ();

' 2005 year one month

Dt. Getdatetimeformats (' D ') [0]. ToString ();

' 2005 5 days a month

Dt. Getdatetimeformats (' D ') [1]. ToString ();

' 2005 11 05

Dt. Getdatetimeformats (' D ') [2]. ToString ();

' Saturday 2005

Dt. Getdatetimeformats (' D ') [3]. ToString ();

' Saturday 2005 5 days a month

Dt. Getdatetimeformats (' M ') [0]. ToString ();

' 5 days a month

Dt. Getdatetimeformats (' f ') [0]. ToString ();

' 2005 year 5 Day 14:06

Dt. Getdatetimeformats (' g ') [0]. ToString ();

' 2005-11-5 14:06

Dt. Getdatetimeformats (' R ') [0]. ToString ();

' Sat, Nov 2005 14:06:25 GMT

String. Format ("{0:d}", DT);

' 2005-11-5

String. Format ("{0:d}", DT);

' 2005 5 days a month

String. Format ("{0:f}", DT);

' 2005 year 5 Day 14:23

String. Format ("{0:f}", DT);

' 2005 year 5 Day 14:23:23

String. Format ("{0:g}", DT);

' 2005-11-5 14:23

String. Format ("{0:g}", DT);

' 2005-11-5 14:23:23

String. Format ("{0:m}", DT);

' 5 days a month

String. Format ("{0:r}", DT);

' Sat, Nov 2005 14:23:23 GMT

String. Format ("{0:s}", DT);

' 2005-11-05t14:23:23

String. Format ("{0:t}", DT);

' 14:23

String. Format ("{0:t}", DT);

' 14:23:23

String. Format ("{0:u}", DT);

' 2005-11-05 14:23:23z

String. Format ("{0:u}", DT);

' 2005 year 5 Day 6:23:23

String. Format ("{0:y}", DT);

' 2005 year one month

String. Format ("{0}", DT);

' 2005-11-5 14:23:23

String. Format ("{0:yyyymmddhhmmssffff}", DT);

Compare two time sizes
1, time-size experiments
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. Calculates the function of two time difference value, returns the absolute value of the time difference:
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 () + "Day"
+ts. Hours.tostring () + "hours"
+ts. Minutes.tostring () + "minutes"
+ts. Seconds.tostring () + "seconds";
}
Catch
{

}
return DateDiff;
}

3. Realize the function of calculating datetime1-36 day =datetime2

TimeSpan ts=new TimeSpan (40,0,0,0);
DateTime dt2=datetime.now.subtract (TS);
Msg. Text=datetime.now.tostring () + "-" +ts. Days.tostring () + "days \ r \ n";
Msg. Text+=dt2. ToString ();

C # Date formatting

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.