formatCode is an optional format code string. (For details, please search for "Format string" to view)
The format must be separated from other characters with "{" and "}". If you also use braces in the format, you can use two consecutive braces to represent a brace, namely: "{{" or "}}".
Common format examples:
(1) int i=12345;
this.=i.ToString();
//Result 12345 (this refers to the current object, or an instance of the current class)
this.=i.ToString("d8");
//Result 00012345
(2) int i=123;
double j=123.45;
string s1=("the value is {0,7:d}",i);
string s2=("the value is {0,7:f3}",j);
this.=s1 ;
//Result the value is 123
this.=s2;
//Result the value is 123.450
(3)double i=12345.6789;
this.=i.ToString("f2"); //Result 12345.68
this.=i.ToString("f6");
//Result 12345.678900
(4)double i=12345.6789;
this.=i.ToString("n"); //Result 12,345.68
this.=i.ToString("n4"); //Result 12,345.6789
(5)double i=0.126;
string s=("the value is {0:p}",i);
this.=i.ToString("p"); //Result 12.6%
this.=s; //Result the value is 12.6%
(6) DateTime dt =new DateTime(2003,5,25);
this.=dt.ToString("");
//Result 03.5.25
this.=dt.ToString("yyyyy year M month");
//Result May 2003
("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"
(7) int i=123;
double j=123.45;
string s=("i:{0,-7},j:{1,7}",i,j);
//-7 means left alignment, occupying 7 digits
this.=s ;
//Result i:123,j: 123.45
DateTime.ToString()Detailed explanation of usage
We often encounter time conversion to achieve different display effects. The default format is: 2006-6-6 14:33:34
What should I do if I want to change it to 200606, 06-2006, 2006-6-6 or more?
Here you will use:Method(String, IFormatProvider)
Example:
using System;
using ;
String format="D";
DateTime date=;
(date.ToString(format, ));
Results output
Thursday, June 16, 2006
Here is a detailed usage of the parameter format format
=======================
Format Characters Associated Attributes/Descriptions
d ShortDatePattern
D LongDatePattern
f Full 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 RFC1123Pattern
s SortableDateTimePattern using local time (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern Format for displaying common time
U Full date and time (long date and long time) using common time
y、Y YearMonthPattern
The following table lists the patterns that can be merged to construct custom patterns
========================================
These patterns are case sensitive; for example, identify "MM", but not "mm". If the custom pattern contains whitespace characters or characters enclosed in single quotes, the output string page will also contain these characters. Characters not defined as part of the format pattern or not defined as format characters are copied in their original meaning.
Format Mode Description:
d One day in the month. A single-digit date has no leading zeros.
dd One day in the month. A single-digit date has a leading zero.
ddd The abbreviation name for a day of the week, defined in AbbreviatedDayNames.
dddd The full name of a day of the week, defined in DayNames.
M Month figure. A single-digit month has no leading zeros.
MM Month figures. A single-digit month has a leading zero.
The abbreviation name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
y does not contain the year of the epoch. If the year that does not contain an epoch is less than 10, a year without leading zeros is displayed.
yy does not contain the year of the epoch. If the year without an epoch is less than 10, the year with leading zeros is displayed.
yyyy includes four-digit year of the epoch.
gg period or epoch. If the date you want to format does not have an associated epoch or epoch string, this pattern is ignored.
h 12-hour hours. A single-digit hour number has no leading zeros.
hh 12-hour hours. The number of hours in a single digit has leading zeros.
H 24-hour hours. A single-digit hour number has no leading zeros.
HH 24-hour hours. The number of hours in a single digit has leading zeros.
m minutes. A single-digit minute number has no leading zeros.
mm minutes. A single-digit minute number has a leading zero.
s seconds. A single-digit number of seconds has no leading zeros.
ss seconds. A single-digit number of seconds has a leading zero.
The decimal accuracy of f seconds is one bit. The rest of the numbers are truncated.
The decimal accuracy of ff seconds is two digits. The rest of the numbers are truncated.
The decimal accuracy of fff seconds is three digits. The rest of the numbers are truncated.
ffff The decimal accuracy of the second is four digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is five digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is six digits. The rest of the numbers are truncated.
ffffffff The decimal accuracy of a second is seven digits. The rest of the numbers are truncated.
t The first character (if present) of the AM/PM indicator defined in AMDesignator or PMDesignator.
tt The AM/PM indicator defined in AMDesignator or PMDesignator (if present).
z Time zone offset ("+" or "-" is followed only by hours). A single-digit hour number has no leading zeros. For example, Pacific Standard Time is "-8".
zz Time zone offset ("+" or "-" is followed only by hours). The number of hours in a single digit has leading zeros. For example, Pacific Standard Time is "-08".
zzz Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes in a single digit has leading zeros. For example, Pacific Standard Time is "-08:00".
: The default time separator defined in TimeSeparator.
/ The default date separator defined in DateSeparator.
%c where c is format mode (if used alone). If the format pattern is merged with the primitive characters or other format pattern, the "%" character can be omitted.
\ c Where c is any character. Show characters according to the original meaning. To display backslash characters, use "\\".
Only the format schema listed in the second table above can be used to create a custom schema; the standard format characters listed in the first table cannot be used to create a custom schema. The length of the custom pattern is at least two characters; for example,
DateTime.ToString( "d") Returns the DateTime value; "d" is the standard short date mode.
DateTime.ToString( "%d") Returns a day in the month; "%d" is a custom mode.
DateTime.ToString( "d ") Returns a day in the month followed by a blank character; "d" is a custom mode.
What is more convenient is that the above parameters can be combined at will and there will be no errors. Try more and you will definitely find the time format you want.
If you want to get the time of this format in June 2005
You can write this way:
date.ToString("yyyyy year MM month", )
And so on.
Here are some specific date formatting usages:
============================================
1. Method for formatting dates during binding:
2. Method for formatting dates of data controls such as DataGrid/DataList:
[0].Text = ([0].Text).ToShortDateString();
3. Use String class to convert the date display format:
( "yyyy-MM-dd ",yourDateTime);
4. Use the Convert method to convert the date display format:
("2005-8-23").ToString
("yyMMdd",); //Support traditional database
5. Use it directlyToStringMethod to convert date display format:
.ToString("yyyyMMddhhmmss");
.ToString("yyyy/MM/dd hh:mm:ss")
6. Show only the year and month
(,"starttime","{0:yyyy-M}")
7. Display all parts of the time, including: year, month, day, hour, minute, and second
DataFormatString='{0:yyyy-MM-dd HH24:mm:ss}'>
Use DateTime.ToString(string format) output dates in different formats
The format must be separated from other characters with "{" and "}". If you also use braces in the format, you can use two consecutive braces to represent a brace, namely: "{{" or "}}".
Common format examples:
(1) int i=12345;
this.=i.ToString();
//Result 12345 (this refers to the current object, or an instance of the current class)
this.=i.ToString("d8");
//Result 00012345
(2) int i=123;
double j=123.45;
string s1=("the value is {0,7:d}",i);
string s2=("the value is {0,7:f3}",j);
this.=s1 ;
//Result the value is 123
this.=s2;
//Result the value is 123.450
(3)double i=12345.6789;
this.=i.ToString("f2"); //Result 12345.68
this.=i.ToString("f6");
//Result 12345.678900
(4)double i=12345.6789;
this.=i.ToString("n"); //Result 12,345.68
this.=i.ToString("n4"); //Result 12,345.6789
(5)double i=0.126;
string s=("the value is {0:p}",i);
this.=i.ToString("p"); //Result 12.6%
this.=s; //Result the value is 12.6%
(6) DateTime dt =new DateTime(2003,5,25);
this.=dt.ToString("");
//Result 03.5.25
this.=dt.ToString("yyyyy year M month");
//Result May 2003
("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"
(7) int i=123;
double j=123.45;
string s=("i:{0,-7},j:{1,7}",i,j);
//-7 means left alignment, occupying 7 digits
this.=s ;
//Result i:123,j: 123.45
DateTime.ToString()Detailed explanation of usage
We often encounter time conversion to achieve different display effects. The default format is: 2006-6-6 14:33:34
What should I do if I want to change it to 200606, 06-2006, 2006-6-6 or more?
Here you will use:Method(String, IFormatProvider)
Example:
using System;
using ;
String format="D";
DateTime date=;
(date.ToString(format, ));
Results output
Thursday, June 16, 2006
Here is a detailed usage of the parameter format format
=======================
Format Characters Associated Attributes/Descriptions
d ShortDatePattern
D LongDatePattern
f Full 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 RFC1123Pattern
s SortableDateTimePattern using local time (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern Format for displaying common time
U Full date and time (long date and long time) using common time
y、Y YearMonthPattern
The following table lists the patterns that can be merged to construct custom patterns
========================================
These patterns are case sensitive; for example, identify "MM", but not "mm". If the custom pattern contains whitespace characters or characters enclosed in single quotes, the output string page will also contain these characters. Characters not defined as part of the format pattern or not defined as format characters are copied in their original meaning.
Format Mode Description:
d One day in the month. A single-digit date has no leading zeros.
dd One day in the month. A single-digit date has a leading zero.
ddd The abbreviation name for a day of the week, defined in AbbreviatedDayNames.
dddd The full name of a day of the week, defined in DayNames.
M Month figure. A single-digit month has no leading zeros.
MM Month figures. A single-digit month has a leading zero.
The abbreviation name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
y does not contain the year of the epoch. If the year that does not contain an epoch is less than 10, a year without leading zeros is displayed.
yy does not contain the year of the epoch. If the year without an epoch is less than 10, the year with leading zeros is displayed.
yyyy includes four-digit year of the epoch.
gg period or epoch. If the date you want to format does not have an associated epoch or epoch string, this pattern is ignored.
h 12-hour hours. A single-digit hour number has no leading zeros.
hh 12-hour hours. The number of hours in a single digit has leading zeros.
H 24-hour hours. A single-digit hour number has no leading zeros.
HH 24-hour hours. The number of hours in a single digit has leading zeros.
m minutes. A single-digit minute number has no leading zeros.
mm minutes. A single-digit minute number has a leading zero.
s seconds. A single-digit number of seconds has no leading zeros.
ss seconds. A single-digit number of seconds has a leading zero.
The decimal accuracy of f seconds is one bit. The rest of the numbers are truncated.
The decimal accuracy of ff seconds is two digits. The rest of the numbers are truncated.
The decimal accuracy of fff seconds is three digits. The rest of the numbers are truncated.
ffff The decimal accuracy of the second is four digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is five digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is six digits. The rest of the numbers are truncated.
ffffffff The decimal accuracy of a second is seven digits. The rest of the numbers are truncated.
t The first character (if present) of the AM/PM indicator defined in AMDesignator or PMDesignator.
tt The AM/PM indicator defined in AMDesignator or PMDesignator (if present).
z Time zone offset ("+" or "-" is followed only by hours). A single-digit hour number has no leading zeros. For example, Pacific Standard Time is "-8".
zz Time zone offset ("+" or "-" is followed only by hours). The number of hours in a single digit has leading zeros. For example, Pacific Standard Time is "-08".
zzz Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes in a single digit has leading zeros. For example, Pacific Standard Time is "-08:00".
: The default time separator defined in TimeSeparator.
/ The default date separator defined in DateSeparator.
%c where c is format mode (if used alone). If the format pattern is merged with the primitive characters or other format pattern, the "%" character can be omitted.
\ c Where c is any character. Show characters according to the original meaning. To display backslash characters, use "\\".
Only the format schema listed in the second table above can be used to create a custom schema; the standard format characters listed in the first table cannot be used to create a custom schema. The length of the custom pattern is at least two characters; for example,
DateTime.ToString( "d") Returns the DateTime value; "d" is the standard short date mode.
DateTime.ToString( "%d") Returns a day in the month; "%d" is a custom mode.
DateTime.ToString( "d ") Returns a day in the month followed by a blank character; "d" is a custom mode.
What is more convenient is that the above parameters can be combined at will and there will be no errors. Try more and you will definitely find the time format you want.
If you want to get the time of this format in June 2005
You can write this way:
date.ToString("yyyyy year MM month", )
And so on.
Here are some specific date formatting usages:
============================================
1. Method for formatting dates during binding:
2. Method for formatting dates of data controls such as DataGrid/DataList:
[0].Text = ([0].Text).ToShortDateString();
3. Use String class to convert the date display format:
( "yyyy-MM-dd ",yourDateTime);
4. Use the Convert method to convert the date display format:
("2005-8-23").ToString
("yyMMdd",); //Support traditional database
5. Use it directlyToStringMethod to convert date display format:
.ToString("yyyyMMddhhmmss");
.ToString("yyyy/MM/dd hh:mm:ss")
6. Show only the year and month
(,"starttime","{0:yyyy-M}")
7. Display all parts of the time, including: year, month, day, hour, minute, and second
DataFormatString='{0:yyyy-MM-dd HH24:mm:ss}'>
Use DateTime.ToString(string format) output dates in different formats