Method: C # Format Data date formatting example format using dataformatstring

Source: Internet
Author: User
Tags iso 8601 truncated

Dataformatstring= "{0: format string}"

The {0} in dataformatstring represents the data itself, and the format string after the colon represents the format that the data is expected to display;

Number, Currency format:
After the specified format symbol, you can specify the number of digits to display for the decimal. For example, the original data is "1.56", and if the format is set to {0:n1}, the output is "1.5". Its commonly used numeric format is shown in the following table:

format string input result
"{0:c}" 12345.6789 $12,345.68
"{0:c}"-12345.6789 ($12,345.68)
"{0:d}" 12345 12345
"{0:d8}" 12345 00012345
"{0:e}" 12345.6789 1234568E+004
"{0:e10}" 12345.6789 1.2345678900E+004
"{0:f}" 12345.6789 12345.68
"{0:f0}" 12345.6789 12346
"{0:g}" 12345.6789 12345.6789
"{0:g7}" 123456789 1.234568E8
"{0:n}" 12345.6789 12,345.68
"{0:n4}" 123456789 123,456,789.0000
' Total: {0:c} ' 12345.6789 total: $12345.68

Common Date-time formats:

Format Description Output format
D Thin Date format mm/dd/yyyy
D Detailed date format dddd, MMMM DD, yyyy
F Full Format (long date + short time) dddd, MMMM dd, yyyy hh:mm
F
Full Date Time format
(Long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
G General format (short date + short time) mm/dd/yyyy hh:mm
G General Format (short date + long time) mm/dd/yyyy HH:mm:ss
M,m month-day format MMMM DD
s medium Date time format Yyyy-mm-dd HH:MM:SS
T Lite time Format hh:mm
T Verbose time Format HH:mm:ss

Date conversion One

In order to achieve different display effects sometimes we need to convert the time, the default format is: 2007-01-03 14:33:34, to convert to another format, to use the DateTime.ToString method (String, IFormatProvider), As shown below:


Using System;
Using System.Globalization;
String format= "D";
DateTime Date=datatime,now;
Response.Write (date. ToString (format, datetimeformatinfo.invariantinfo));


Result output
Thursday, June 16, 2005


Parameter format verbose usage:

Format Character Association properties/Description
D ShortDatePattern
D Longdatepattern
F Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
M, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U universalsortabledatetimepattern format for displaying the Universal Time
U use the full date and time of the universal Time (long date and long time)
Y, y Yearmonthpattern

The following table lists the patterns that can be combined to construct a custom pattern. These patterns are case-sensitive, for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space characters or characters enclosed in single quotation marks, the output string page will also contain these characters. Characters that are not defined as part of the format pattern or that are not defined as format characters are copied verbatim.

Format Pattern Description
A day in the D month. One-digit date has no leading zeros.
DD a day of the month. A one-digit date has a leading zero.
The abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, as defined in DayNames.
M-month number. One-digit month has no leading zeros.
MM month number. One-digit month has a leading zero.
Abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, as defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
YY does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
The YYYY includes the four-digit year of the era.
GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12 Hour hour system. One-digit hours do not have leading zeros.
HH 12-hour hour. One-digit hours have leading zeros.
H 24 hour hour system. One-digit hours do not have leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.
M minutes. A single-digit number of minutes does not have a leading zero.
MM minutes. A single-digit number of minutes 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 of one digit has a leading zero.
The fractional precision of the F-second is one digit. The remaining digits are truncated.
The fractional precision of the FF seconds is two bits. The remaining digits are truncated.
The fractional precision of FFF seconds is three bits. The remaining digits are truncated.
The fractional precision of ffff seconds is four bits. The remaining digits are truncated.
The fractional precision of fffff seconds is five bits. The remaining digits are truncated.
The fractional precision of ffffff seconds is six bits. The remaining digits are truncated.
The fractional precision of fffffff seconds is seven bits. The remaining digits are truncated.
t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
The AM/PM indicated by the TT in AMDesignator or PMDesignator (if present).
The Z Time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8".
ZZ Time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08".
The ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00".
: The default time delimiter defined in TimeSeparator.
/The default date delimiter defined in DateSeparator.
% c where C is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns.
\ c where c is any character. Displays the characters as they are originally defined. To display a backslash character, use "\ \".

Only the format patterns listed in the second table above can be used to create custom patterns, and the standard format characters listed in the first table cannot be used to create custom patterns. The custom pattern is at least two characters long;

DateTime.ToString ("D") returns a DateTime value; "D" is a standard short date pattern.
DateTime.ToString ("%d") returns the day of the month; "%d" is a custom pattern.
DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern.

More convenient is that the above parameters can be arbitrarily combined, and will not be wrong, try more, will definitely find the time format you want
To get the time in this format for June 2005
It can be written like this:
Date. ToString ("yyyy mm month", Datetimeformatinfo.invariantinfo)

Date conversion Two


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
Label6.text = dt. Getdatetimeformats (' D ') [2]. ToString ();//week 62,005 for
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);

Method: C # Format Data date formatting sample format using dataformatstring

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.