C # Date function [string. Format----getdatetimeformats]. "Reprint for Reference"

Source: Internet
Author: User

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, 2005 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, 2005 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);
Yyyymm can be set, such as Label16.text = string. Format ("{0:YYYYMMDD}", DT);

The DateTime.ToString () function has four overloads. Generally used much is the one without parameters. As everyone knows, datetime.tostring (string format) is more powerful and can output dates in different formats. Some of the following are listed for your reference. Some of them are not listed on MSDN.

1. Y represents the year, note is lowercase y, and uppercase y does not represent the year.

2. m represents the month.

3. D represents the date, noting that D does not mean anything.

4. h or H represents hours, H is 12 hours, H is 24 hours.

5. m represents minutes.

6. s represents seconds. Note that s doesn't mean anything.

Format

Output

Example

Years

Y

7

String yy = DateTime.Now.ToString ("y-mm")

Yy= "7-05"

Yy

07

String yy = DateTime.Now.ToString ("yy-mm")

Yy= "07-05"

yyy or more Y

1984

String yy = DateTime.Now.ToString ("yyyy");

Yy= "2007"

Month

M

5.

String mon = DateTime.Parse ("1984-05-09") ToString ("Yyyy-m")

Mon = "1984-5"

Mm

05.

String mon = DateTime.Parse ("1984-05-09") ToString ("MM")

Mon = "05"

MMM

If it is the Chinese version of the operating system, it will be output: May.

If it is an English operating system, enter the first three letters of the month: May

String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")

English version of operating system: Jul

Chinese version of the operating system: July

Mmmm or more M

If it is the Chinese version of the operating system, it will be output: May.

If it is an English operating system, enter the full write of the month

String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")

English version of operating system: July

Chinese version of the operating system: July

Date or day of the week

D

9

String dd= datetime.parse ("1984-05-09") ToString ("D")

Dd= "9"

Dd

09

String dd= datetime.parse ("1984-05-09") ToString ("DD")

dd= "09"

Ddd

If it is the Chinese version of the operating system, it will output the week, such as Wednesday.

If it is an English operating system, the output Week is abbreviated:

Wed

string dd = DateTime.Parse ("2006-07-01"). ToString ("ddd")

English version of operating system: Wed

Chinese version of the operating system: Wednesday

dddd or more D

If it is the Chinese version of the operating system, it will output the week, such as Wednesday.

If it is an English operating system, the output week:

Wednesday

string dd = DateTime.Parse ("2006-07-01"). ToString ("dddd")

English version of operating system: Wednesday

Chinese version of the operating system: Wednesday

Hours

H

Hour Range: 1-12

string hh = DateTime.Now.ToString ("h");

HH = 8

HH or more H

Hour Range: 1-12

string hh = DateTime.Now.ToString ("hh");

HH = 08

H

Hour Range: 0-23

string hh = DateTime.Now.ToString ("yyyy-h");

HH = 2006-8

HH or more H

Hour Range: 0-23

string hh = DateTime.Now.ToString ("yyyy-hh");

HH = 2006-08

string hh = Datetime.pare ("2006-7-4 18:00:00"). ToString ("yyyy-hh");

HH = 2006-18

Minutes

M

6

string mm = DateTime.Now.ToString ("yyyy-mm-dd-m");

MM = "2006-07-01-6";

mm or more M

06

string mm = DateTime.Now.ToString ("yyyy-mm-dd-mm");

MM = "2006-07-01-06";

Seconds

S

6

string mm = DateTime.Now.ToString ("Yyyy-mm-dd-s");

MM = "2006-07-01-6";

SS or more S

06

string mm = DateTime.Now.ToString ("Yyyy-mm-dd-ss");

MM = "2006-07-01-06";

C # Date function [string. Format----getdatetimeformats]. "Reprint for Reference"

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.