1) date format display
In SQL, DateTime is an 8-bit length date in the format of 00:00:00
In ASP. NET, only part of the 2003-12-31 is commonly used. You can use
DateTime. Now. ToString ("d") or DateTime. Now. to1_datestring ();
However, the specific format is either 2003/12/31 or 2003-12-31 depends on which system configuration date is configured.
Use the. Tostring () User-Defined Format if you want to confirm that the format specified in 2003-12-31 is obtained.
For the characters in the format, see the document above.
For example, if. ToString ("yyyy-MM-dd") is returned
. ToString ("yyyyMMdd") returns 20031125
D. display the day as a number without leading zero (for example, 1 ). Use
% D.
Dd displays the day as a number with a leading zero (for example, 01 ).
Ddd displays the day as an abbreviation (for example, Sun ).
Dddd displays the day as the full name (for example, Sunday ).
M Displays the month as a number without leading zero (for example, January 1, January ). If this is unique in the User-Defined number format
Character, use % M.
MM displays the month as a number with a leading zero (for example, 01/12/01 ).
MMM displays the month as an abbreviation (for example, Jan ).
MMMM displays the month as the complete month name (for example, January ).
Gg display the time/epoch string (for example, A. D .)
H uses the 12-hour system to display the hour as a number without leading zero (for example, 1:15:15 ). If this is a user-defined number
The only character in the Word format. Use % h.
Hh uses the 12-hour system to display the hour as a number with a leading zero (for example, 01:15:15 ).
H uses the 24-hour system to display the hour as a number without leading zero (for example, 1:15:15 ). If this is a user-defined number
The only character in the format. Use % H.
HH uses the 24-hour system to display the hour as a number with a leading zero (for example, 01:15:15 ).
M Displays the minute as a number without leading zero (for example ). If this is the only word in the User-Defined Number Format
Use % m.
Mm displays the minute as a number with a leading zero (for example, 12:01:15 ).
S displays the second as a number without leading zero (for example ). If this is a unique character in the User-Defined numeric format
, Use % s.
Ss displays seconds as numbers with leading zeros (for example, 12:15:05 ).
F displays the fractional part of the second. For example, ff is precisely displayed to 1% seconds, while ffff is precisely displayed to one in ten seconds.
Up to seven f characters can be used in a user-defined format. If this is a unique character in the User-Defined number format, use % f.
T uses the 12-hour format, and displays uppercase A for any hour before noon, for any small value between noon and p.m.
The upper-case P is displayed. If this is a unique character in the User-Defined number format, use % t.
Tt uses the 12-hour format, and displays uppercase values for any hour before noon; for any small value between noon and p.m.
The uppercase PM is displayed.
Y displays the year (0-9) as a number without leading zero. Use
% Y.
Yy displays the year in the format of two digits with leading zeros (if applicable ).
Yyy displays the year in four-digit format.
Yyyy displays the year in four-digit format.
Z shows the time zone offset without leading zero (for example,-8 ). Use
% Z.
Zz displays the time zone offset with leading zero (for example,-08)
Zzz displays the complete time zone offset (for example)
You can also format the time in this way. You can use a custom format to set the format output you want.
You can use the characters in the preceding table to make any combination regardless of the order. For example, you can write yyyy-yyyy.
Note: The format must be DateTime format. The types in SQL are the same. Otherwise, the format is invalid or incorrect. This is especially used in the format of {0: IFromat} in the DataGrid.
2) Date input
DateTime. Pares ()
However, the conversion in this form is quite limited. Some C # will not understand the date format you write, for example, 20031231, Which is 2003-
No. 12-31. C # Don't know him. We can do the following:
// Set the language country
System. IFormatProvider format = new System. Globalization. CultureInfo ("zh-CN", true );
// Specify the conversion format
Response. Write (DateTime. ParseExact (this. TextBox1.Text, "yyyyMMdd", format ));
In this way, he can see the date format of "20031231 ".
However, it is a pity that he can only convert the yyyyMMdd format and cannot convert other formats. Isn't that perfect?
However,. NET provides another type of overloaded version.
Public static DateTime ParseExact (string, string [], IFormatProvider, DateTimeStyles );
Converts specified date formats.