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 the 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 Associated Attributes/Descriptions
d ShortDatePattern -----08/30/2006
D LongDatePattern -----Wednesday, 30 August 2006
f Full date and time (long date and short time) -----Wednesday, 30 August 2006 23:21
F FullDateTimePattern (Long Date and Long Time) -----Wednesday, 30 August 2006 23:22:02
g Regular (short date and short time) -----08/30/2006 23:22
G Regular (short date and long time) ----08/30/2006 23:23:11
m、M MonthDayPattern
r、R RFC1123Pattern
s SortableDateTimePattern using local time (based on ISO 8601)
t ShortTimePattern ------23:24
T LongTimePattern -------23:24:30
u UniversalSortableDateTimePattern Format for displaying common time -------2006-08-30 23:25:10Z
U Full date and time (long date and long time) using common time -----Wednesday, 30 August 2006 15:25:37
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. 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 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,
( "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 of this format in June 2005
You can write this way:
("yyyyy year MM month", )
And so on
("yyyy/MM/dd HH:mm:ss", )
//today
();
//Yesterday, today's date is reduced by one
(-1).ToShortDateString();
//Tomorrow, same is true, add one
(1).ToShortDateString();
//This week (To know the first day of this week, you must first know what day of the week is, so that the first day of this week is the day a few days ago. It should be noted that every week here starts from Sunday to Saturday
(((0 - Convert.ToInt16()))).ToShortDateString();
(((6 - Convert.ToInt16()))).ToShortDateString();
//If you don't understand, you should understand it by looking at the Chinese method of displaying the day of the week
// Since DayOfWeek returns a number of days of the week, we need to convert it into Chinese characters for us to read. Some people may use switch to compare one by one, but in fact, it doesn't have to be so troublesome.
string[] Day = new string[] { "Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday" };
Day[Convert.ToInt16()];
//Last week, similarly, one week was 7 days, last week was minus 7 days this week, and the same is true for next week.
(((0 - Convert.ToInt16())) - 7).ToShortDateString();
(((6 - Convert.ToInt16())) - 7).ToShortDateString();
//next week
(((0 - Convert.ToInt16())) + 7).ToShortDateString();
(((6 - Convert.ToInt16())) + 7).ToShortDateString();
//This month, many people will say that the first day of this month must be the first day, and the last day is to reduce the next day on the first day. Of course this is correct
//General writing
() + () + "1"; //The first day
(() + () + "1").AddMonths(1).AddDays(-1).ToShortDateString();//Last day
//Skillfully formatting the ToString characters in C# is easier
("yyyy-MM-01");
(("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
//Last month, minus one month
(("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
(("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Next month, add one month
(("yyyy-MM-01")).AddMonths(1).ToShortDateString();
(("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
//7 days later
();
(7).ToShortDateString();
//7 days ago
(-7).ToShortDateString();
();
//For this year, we can easily calculate the first and last days of the year with ToString's characters formatting.
(("yyyy-01-01")).ToShortDateString();
(("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
//No need to explain last year
(("yyyy-01-01")).AddYears(-1).ToShortDateString();
(("yyyy-01-01")).AddDays(-1).ToShortDateString();
//Next year
(("yyyy-01-01")).AddYears(1).ToShortDateString();
(("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
//This quarter, many people will find it difficult here and need to write a long process to judge. Actually, it doesn't work. We all know that four quarters a year, three months a quarter
//First of all, we push the date to the first month of this quarter, and then the first day of this month is the first day of this quarter
(0 - (( - 1) % 3)).ToString("yyyy-MM-01");
//Similarly, the last day of this quarter is the first day of next quarter to reduce one
((3 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//I believe you all know about it next quarter. . . . knock off
(3 - (( - 1) % 3)).ToString("yyyy-MM-01");
((6 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Last quarter
(-3 - (( - 1) % 3)).ToString("yyyy-MM-01");
((0 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
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 the 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 Associated Attributes/Descriptions
d ShortDatePattern -----08/30/2006
D LongDatePattern -----Wednesday, 30 August 2006
f Full date and time (long date and short time) -----Wednesday, 30 August 2006 23:21
F FullDateTimePattern (Long Date and Long Time) -----Wednesday, 30 August 2006 23:22:02
g Regular (short date and short time) -----08/30/2006 23:22
G Regular (short date and long time) ----08/30/2006 23:23:11
m、M MonthDayPattern
r、R RFC1123Pattern
s SortableDateTimePattern using local time (based on ISO 8601)
t ShortTimePattern ------23:24
T LongTimePattern -------23:24:30
u UniversalSortableDateTimePattern Format for displaying common time -------2006-08-30 23:25:10Z
U Full date and time (long date and long time) using common time -----Wednesday, 30 August 2006 15:25:37
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. 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 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,
( "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 of this format in June 2005
You can write this way:
("yyyyy year MM month", )
And so on
("yyyy/MM/dd HH:mm:ss", )
//today
();
//Yesterday, today's date is reduced by one
(-1).ToShortDateString();
//Tomorrow, same is true, add one
(1).ToShortDateString();
//This week (To know the first day of this week, you must first know what day of the week is, so that the first day of this week is the day a few days ago. It should be noted that every week here starts from Sunday to Saturday
(((0 - Convert.ToInt16()))).ToShortDateString();
(((6 - Convert.ToInt16()))).ToShortDateString();
//If you don't understand, you should understand it by looking at the Chinese method of displaying the day of the week
// Since DayOfWeek returns a number of days of the week, we need to convert it into Chinese characters for us to read. Some people may use switch to compare one by one, but in fact, it doesn't have to be so troublesome.
string[] Day = new string[] { "Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday" };
Day[Convert.ToInt16()];
//Last week, similarly, one week was 7 days, last week was minus 7 days this week, and the same is true for next week.
(((0 - Convert.ToInt16())) - 7).ToShortDateString();
(((6 - Convert.ToInt16())) - 7).ToShortDateString();
//next week
(((0 - Convert.ToInt16())) + 7).ToShortDateString();
(((6 - Convert.ToInt16())) + 7).ToShortDateString();
//This month, many people will say that the first day of this month must be the first day, and the last day is to reduce the next day on the first day. Of course this is correct
//General writing
() + () + "1"; //The first day
(() + () + "1").AddMonths(1).AddDays(-1).ToShortDateString();//Last day
//Skillfully formatting the ToString characters in C# is easier
("yyyy-MM-01");
(("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
//Last month, minus one month
(("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
(("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Next month, add one month
(("yyyy-MM-01")).AddMonths(1).ToShortDateString();
(("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
//7 days later
();
(7).ToShortDateString();
//7 days ago
(-7).ToShortDateString();
();
//For this year, we can easily calculate the first and last days of the year with ToString's characters formatting.
(("yyyy-01-01")).ToShortDateString();
(("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
//No need to explain last year
(("yyyy-01-01")).AddYears(-1).ToShortDateString();
(("yyyy-01-01")).AddDays(-1).ToShortDateString();
//Next year
(("yyyy-01-01")).AddYears(1).ToShortDateString();
(("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
//This quarter, many people will find it difficult here and need to write a long process to judge. Actually, it doesn't work. We all know that four quarters a year, three months a quarter
//First of all, we push the date to the first month of this quarter, and then the first day of this month is the first day of this quarter
(0 - (( - 1) % 3)).ToString("yyyy-MM-01");
//Similarly, the last day of this quarter is the first day of next quarter to reduce one
((3 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//I believe you all know about it next quarter. . . . knock off
(3 - (( - 1) % 3)).ToString("yyyy-MM-01");
((6 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Last quarter
(-3 - (( - 1) % 3)).ToString("yyyy-MM-01");
((0 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();