C# time/date format collection, C# time/date function collection
Sometimes we have to convert time to achieve different display effects.
The default format is: 2005-6-6 14:33:34
What should I do if I want to change it to 200506, 06-2005, 2005-6-6 or more?
We want to use:
Method (String, IFormatProvider)
using System;
using ;
String format="D";
DateTime date=DataTime,Now;
((format, ));
Results output Thursday, June 16, 2005
Detailed usage of parameter format:
Format Characters �
d ShortDatePattern
D LongDatePattern
f
F
g
G
m、M MonthDayPattern
r、R FC1123Pattern
s �
t ShortTimePattern
T LongTimePattern
u �
U
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 A single-digit date has no leading zeros.
dd � A single-digit date has a leading zero.
ddd
dddd The full name of a day in the week, defined in DayNames.
M A single-digit month has no leading zeros.
MM � A single-digit month has a leading zero.
MMM The abbreviation name of the month, defined in AbbreviatedMonthNames.
MMMM The full name of the month, defined in MonthNames.
y � If the year that does not contain an epoch is less than 10, a year without leading zeros is displayed.
yy If the year without an epoch is less than 10, the year with leading zeros is displayed.
yyyy
gg � If the date you want to format does not have an associated epoch or epoch string, this pattern is ignored.
h A single-digit hour number has no leading zeros.
hh � The number of hours in a single digit has leading zeros.
H A single-digit hour number has no leading zeros.
HH � The number of hours in a single digit has leading zeros.
m A single-digit minute number has no leading zeros.
mm A single-digit minute number has a leading zero.
s � The number of seconds in a single digit has no leading zeros.
ss A single-digit second has a leading zero.
f The rest of the numbers are truncated.
ff The decimal accuracy of a second is two digits. The rest of the numbers are truncated.
ffff The rest of the numbers are truncated.
ffff 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 of the AM/PM indicator defined in AMDesignator or PMDesignator (if present).
tt z � 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 The number of hours and minutes in a single digit has leading zeros. For example, Pacific Standard Time is "-08:00".
: �
/
%c where c is the 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 pattern listed in the second table above can be used to create custom patterns;
The standard format characters listed in the first table cannot be used to create custom schemas.
The length of the custom pattern is at least two characters;
For example, ("d") returns the DateTime value;
"d" is the standard short date mode.
( "%d") Returns a day in the month;
"%d" is a custom mode.
( "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 in June 2005, this format.
You can write this way: ("yyyyy year MM month", ) and so on
Here we have introduced how to format the desired date and time format in C#.