Asp.net C # time/date format

Source: Internet
Author: User
Tags datetime iso 8601 local time string format truncated

We need to use:

Datetime. tostring method (string, iformatprovider)

Using system;

Using system. globalization;

 

String format = "d ";

Datetime date = datatime, now;

Response. write (date. tostring (format, datetimeformatinfo. invariantinfo ));

Output Thursday, June 16,200 5

 

Detailed usage of the format parameter:

Format character Association attributes/description

D shortdatepattern

D longdatepattern

F complete date and time (long date and short time)

F fulldatetimepattern (long date and long time)

G regular (short date and short time)

G regular (short date and long time)

M, m monthdaypattern

R, r fc1123pattern

S uses sortabledatetimepattern of the local time (based on iso 8601)

T shorttimepattern

T longtimepattern

U universalsortabledatetimepattern is used to display the format of common time

U uses the complete date and time of the general time (long date and long time)

Y, y yearmonthpattern

 

The following table lists the modes that can be merged to build a custom mode.

These modes are case sensitive. For example, "mm" is recognized, but "mm" is not recognized ".

If the custom mode contains white space characters or characters enclosed in single quotes,

The output string page also contains these characters.

Characters that are not defined as part of the format or are not defined as formatted characters are copied according to their original meaning.

 

Format Description

D. A single-digit date has no leading zero.

A day in dd Month. A single-digit date has a leading zero.

The abbreviation of a day in ddd Week, which is defined in abbreviateddaynames.

The full name of the day of the dddd Week, which is defined in daynames.

M month number. One-digit month does not have a leading zero.

Mm month number. A single-digit month has a leading zero.

The abbreviated name of the mmm month, which is defined in abbreviatedmonthnames.

The full name of the mmmm month, which is defined in monthnames.

Y does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year that does not have the leading zero is displayed.

Yy does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year with the leading zero is displayed.

Yyyy includes the four-digit year of the epoch.

Gg period or epoch. This mode is ignored if the date to be formatted does not have an associated period or epoch string.

H 12-hour. One-digit hours do not have a leading zero.

The hour in hh 12-hour format. The number of hours in a single digit has a leading zero.

H in 24-hour format. One-digit hours do not have a leading zero.

The hour in the 24-hour format. The number of hours in a single digit has a leading zero.

M minutes. The number of minutes in a single digit does not start with zero.

Mm minutes. The number of minutes in a single digit has a leading zero.

S seconds. The number of seconds in a single digit does not have a leading zero.

Ss seconds. The number of seconds in a single digit has a leading zero.

The decimal precision of second f is one digit. The remaining numbers are truncated.

The decimal precision of ff seconds is two digits. The remaining numbers are truncated.

The decimal precision of fff seconds is three. The remaining numbers are truncated.

The decimal precision of ffff seconds is four digits. The remaining numbers are truncated.

The decimal precision of fffff seconds is five digits. The remaining numbers are truncated.

The decimal precision of ffffff seconds is six digits. The remaining numbers are truncated.

The decimal precision of fffff seconds is seven digits. The remaining numbers are truncated.

T the first character (if any) of the am/pm indicator defined in amdesignator or pmdesignator ).

The am/pm indicator defined by tt in amdesignator or pmdesignator (if any ). Z time zone offset ("+" or "-" is followed by only the hour ). One-digit hours do not have a leading zero. For example, the standard time in the Pacific is-8 ".

Zz time zone offset ("+" or "-" is followed by only the hour ). The number of hours in a single digit has a leading zero. For example, the standard time in the Pacific is-08 ".

The full time zone offset of zzz ("+" or "-" followed by hours and minutes ). The number of hours and minutes in a single digit has a leading zero. For example, the Pacific Standard Time is ".

: Default time delimiter defined in timeseparator.

/Default date delimiter defined in dateseparator.

% C where c is the format mode (if used separately ). If the format mode is combined with the original and other formats, the "%" character can be omitted.

C, where c is any character. Display characters according to the original meaning. To display the backslash character, use "".

 

Only the format modes listed in the second table can be used to create custom modes;

The standard format characters listed in the first table cannot be used to create a custom mode.

The length of the custom mode must be at least two characters;

For example, datetime. tostring ("d") returns the datetime Value;

"D" is the standard short date mode.

Datetime. tostring ("% d") returns the day of the month;

"% D" is the custom mode.

Datetime. tostring ("d") returns the day of the month followed by a blank character;

"D" is the custom mode.

It is more convenient that the above parameters can be combined at will without errors. If you try more, you will surely find the time format you want, such as June 2005.

It can be written as follows: date. tostring ("yyyy-mm", datetimeformatinfo. invariantinfo) and so on

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 (); // 21:21:25
Label5.text = dt. tolongdatestring (). tostring (); // January 1, November 5, 2005
Label6.text = dt. tolongtimestring (). tostring (); // 13:21:25
Label7.text = dt. tooadate (). tostring (); // 38661.5565508218
Label8.text = dt. Tow.datestring (). tostring (); // 2005-11-5
Label9.text = dt. Tow.timestring (). tostring (); //
Label10.text = dt. touniversaltime (). tostring (); // 5:21:25
? 2005-11-5 13:30:28. 4412864
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 (); // 13:47:04
Label3.text = dt. adddays (1.1). tostring (); // 16:11:04
Label4.text = dt. addhours (1.1). tostring (); // 14:53:04
Label5.text = dt. addmilliseconds (1.1). tostring (); // 13:47:04
Label6.text = dt. addmonths (1). tostring (); // 13:47:04
Label7.text = dt. addseconds (1.1). tostring (); // 13:47:05
Label8.text = dt. addminutes (1.1). tostring (); // 13:48:10
Label9.text = dt. addticks (1000). tostring (); // 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 ("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 (); //
Label3.text = dt. getdatetimeformats ('Y') [0]. tostring (); // January 1, 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, January 1, November 5, 2005
Label8.text = dt. getdatetimeformats ('M') [0]. tostring (); // January 1, November 5
Label9.text = dt. getdatetimeformats ('F') [0]. tostring (); // November 5, 2005
Label10.text = dt. getdatetimeformats ('g') [0]. tostring (); //
Label11.text = dt. getdatetimeformats ('r') [0]. tostring (); // sat, 05 nov 2005 14:06:25 gmt
Label1.text =? String. format ("{0: d}", dt); // 2005-11-5
Label2.text =? String. format ("{0: d}", dt); // January 1, November 5, 2005
Label3.text =? String. format ("{0: f}", dt); // November 5, 2005
Label4.text =? String. format ("{0: f}", dt); // November 5, 2005 14:23:23
Label5.text =? String. format ("{0: g}", dt); //
Label6.text =? String. format ("{0: g}", dt); // 2005-11-5 14:23:23
Label7.text =? String. format ("{0: m}", dt); // January 1, November 5
Label8.text =? String. format ("{0: r}", dt); // sat, 05 nov 2005 14:23:23 gmt
Label9.text =? String. format ("{0: s}", dt); // 2005-11-05t14: 23: 23
Label10.text = string. format ("{0: t}", dt); //
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); // January 1, November 2005
Label15.text = string. format ("", dt); // 14:23:23?
Label16.text = string. format ("{0: yyyymmddhhmmssffff}", dt); // can be set, such as label16.text = string. format ("{0: yyyymmdd}", dt );

 

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.