Several format conversion methods for time in C

  • 2020-05-19 05:37:22
  • OfStack

Sometimes we need to convert the C# time to achieve a different display

The default format is :2005-6-614:33:34

What if I want to change it to 2006,06 -- 2005,2005-6-6 or more

We will use the method DateTime.ToString (String,IFormatProvider).


usingSystem; 
usingSystem.Globalization; 
Stringformat="D"; 
DateTimedate=DataTime,Now; 
Response.Write(date.ToString(format,DateTimeFormatInfo.InvariantInfo));

Results output

Thursday,June16,2005

The parameter format format is used in detail

Format character association properties/descriptions

dShortDatePattern

DLongDatePattern

f full date and time (long date and short time)

FFullDateTimePattern (long date and long time)

g general (short date and short time)

G general (short date and long time)

m, MMonthDayPattern

r, RRFC1123Pattern

s USES SortableDateTimePattern local time (based on ISO8601)

tShortTimePattern

TLongTimePattern

uUniversalSortableDateTimePattern is used to display a common time format

Full date and time for U using common time (long date and long time)

y, YYearMonthPattern

The following table lists patterns that can be combined to construct custom patterns. These patterns are case sensitive; For example, identify "MM" but not "mm". If the custom pattern contains white space characters or characters enclosed in single quotes, the output string page will also contain these characters. A character that is not defined as part 1 of a format pattern or as a format character is copied in its original meaning.

C# time format pattern description

A day in the month d. A 1-digit date has no leading zero.
A day in the month of dd. A 1-digit date has a leading zero.
The abbreviation for a day of the week defined in AbbreviatedDayNames.
The full name of a day in the week of dddd, defined in DayNames.
M month figures. One-digit months have no leading zeros.
Month number of MM. A 1-digit month has a leading zero.
The abbreviated name of the month MMM, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
y does not include epoch years. If the year without epoch is less than 10, the year without leading zero is displayed.
yy does not include epoch years. If the year without epoch is less than 10, the year with leading zero is displayed.
yyyy includes the 4-digit year of the era.
gg period or era. If you want to format a date that does not have an associated period or epoch string, you ignore the pattern.
h 12-hour system. The number of one-digit hours has no leading zero.
Es112 en 12-hour system. The number of one-digit hours has a leading zero.
H24 hour system. The number of one-digit hours has no leading zero.
HH24 hour system. The number of one-digit hours has a leading zero.
m minutes. There is no leading zero for the number of minutes of one digit.
mm minutes. The number of minutes in a 1-digit number has a leading zero.
s seconds. The number of seconds in a 1-digit number has no leading zero.
ss seconds. The number of seconds in a 1-digit number has a leading zero.
f seconds has a decimal precision of 1 bit. The remaining Numbers are truncated.
ff seconds has a decimal precision of two digits. The remaining Numbers are truncated.
fff seconds has a decimal precision of 3 digits. The remaining Numbers are truncated.
ffff seconds has a decimal precision of 4 digits. The remaining Numbers are truncated.
fffff seconds has a decimal precision of 5 digits. The remaining Numbers are truncated.
ffffff seconds has a decimal precision of 6 digits. The remaining Numbers are truncated.
fffffff seconds has a decimal precision of 7 digits. The remaining Numbers are truncated.
The first character, if present, of the AM/PM indicator defined in AMDesignator or PMDesignator.
tt the AM/PM indicator, if present, defined in AMDesignator or PMDesignator.
z time zone offset (" + "or" - "followed only by hours). The number of one-digit hours has no leading zero. Pacific standard time, for example, is "-8".
zz time zone offset (" + "or" - "followed only by hours). The number of one-digit hours has a leading zero. For example, Pacific standard time is "-08".
zzz full time zone offset (hours and minutes followed by "+" or "-"). The number of hours and minutes of one 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). If the format pattern merges with the literal character or another format pattern, the '%' character can be omitted.
c where c is any character. Display characters as they appear. To display backslash characters, use "".
Only the format schema listed in table 2 above can be used to create a custom schema; The standard format characters listed in table 1 cannot be used to create custom schemas. The length of a 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 to a day in the month; "%d" is a custom pattern.
DateTime. ToString("d") returns a day in the month followed by a blank character; "d" is a custom pattern.
What is more convenient is that the above parameters can be combined at will, and will not be wrong, try more, you will find the time format you want
To get C# time in June 2005 format

C# time can be written like this:

date.ToString ("yyyy MM month ", DateTimeFormatInfo.InvariantInfo)


Related articles: