Sometimes we have to convert C# time to achieve different display effects
The default format is: 2005-6-614: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 the method (String, IFormatProvider)
usingSystem;
;
Stringformat="D";
DateTimedate=DataTime,Now;
((format,));
Results output
Thursday,June16,2005
Detailed usage of parameter format
Format character association properties/description
dShortDatePattern
DLongDatePattern
fFull date and time (long date and short time)
FFullDateTimePattern (Long date and long time)
g Regular (short date and short time)
G regular (short date and long time)
m、MMonthDayPattern
r、RRFC1123Pattern
s uses SortableDateTimePattern for local time (based on ISO8601)
tShortTimePattern
TLongTimePattern
uUniversalSortableDateTimePattern format for displaying common time
U Use full date and time for common time (long date and long time)
y、YYearMonthPattern
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.
C# time format mode description
One day in the month d. A single-digit date has no leading zeros.
dd a day in the month. A single-digit date has a leading zero.
ddd's abbreviation name for a day in the middle of the week is defined in AbbreviatedDayNames.
ddddd's full name for a day in the week is defined in DayNames.
M month figures. A single-digit month has no leading zeros.
MM month figures. A single-digit month has a leading zero.
The abbreviation name of 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 that does not include an epoch is less than 10, the year with leading zeros is displayed.
yyyy includes four-digit years 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.
h12-hour hours. A single-digit hour number has no leading zeros.
hh12-hour hours. The number of hours in a single digit has leading zeros.
H24-hour hours. A single-digit hour number has no leading zeros.
HH24-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. The number of seconds in a single digit has no leading zeros.
ss seconds. A single-digit second 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 ffff seconds is three digits. The rest of the numbers are truncated.
The decimal accuracy of ffff seconds is four digits. The rest of the numbers are truncated.
The decimal accuracy of ffffff seconds is five digits. The rest of the numbers are truncated.
The decimal accuracy of ffffff seconds is six digits. The rest of the numbers are truncated.
The decimal accuracy of ffffffff seconds is seven digits. The rest of the numbers are truncated.
tThe first character of the AM/PM indicator defined in AMDesignator or PMDesignator (if present).
tt 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.
/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,
("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 C# time in this format in June 2005
C# time can be written like this:
("yyyyy year MM month",)