A little understanding of date display format strings

巴扎黑
Release: 2016-12-20 17:16:55
Original
1300 people have browsed it

http://www.cnblogs.com/longsan/archive/2006/04/05/367462.html

Today a netizen posted the following piece of code on the Q group:

strings=Calendar1.TodaysDate.ToString(" d");

I want to get the display method of "MM/dd/yyyy", but it always displays "04-01-2006" instead of the desired "04/01/2006".

At first I also took it for granted that "MM/dd/yyyy" (custom format string) or use "d"
(standard format string) to complete, but after experiments, I found that it always displays "04-01 -2006". Later, I checked the help and got the following understanding of this problem:

1. The display of standard format strings is related to the local area on the machine where the program is running, that is, related to CultrueInfo. For example, in the local regional settings on your machine, the separator between set dates is "-", then what you display through the above code will always be "-", unless you modify the local regional settings.

2. The default display mode of the above code is "04/01/2006" in American English, "04-01-2006" in Chinese, and "04.01.2006" in German.

3. If you want to display "04/01/2006" without modifying the local area settings, you should use a custom format string. The above code can be written as follows:

strings=Calendar1. TodaysDate.ToString("MM'/'dd'/'yyyy");


The two single quotes enclosed are any strings, which will be displayed directly when displayed.

Several solutions:

The first one is as mentioned in 3 above.

Second

In Web.Config, add:

This is the most convenient , but I haven’t tried it.

The third type

Control Panel->Regional Options->Customize (button to the right of time)

It is possible, but there is no portability.

For some reference information:

Date formatting display
In SQL, DateTime is an 8-digit date. The specified format is 2003-12-31 00:00:00
The most commonly used one on ASP.NET is only part of the 2003-12-31 number. You can Use
DateTime.Now.ToString("d") or DateTime.Now.ToShortDateString();
But whether the specific format is 2003/12/31 or 2003-12-31 depends on your system configuration date configuration. .
If you want to be sure to get the 2003-12-31 specification, use .Tostring() user-defined format
For format characters, you can see the document mentioned above.
For example, .ToString("yyyy-MM-dd") returns 2003 -11-25
.ToString("yyyyMMdd") returns 20031125

d Displays the day as a number without leading zeros (such as 1). If this is the only character in a user-defined number format, use

%d.
dd Displays the day as a number with leading zeros (like 01).
ddd Displays the day in its abbreviated form (e.g. Sun).
dddd Display the day as its full name (e.g. Sunday).
M Displays the month as a number without leading zeros (e.g. January is represented as 1). If this is the only

character in a user-defined number format, use %M.
MM displays the month as a number with leading zeros (e.g. 01/12/01).
MMM displays the month as an abbreviation (e.g. Jan).
MMMM Displays the month as its full month name (e.g. January).
gg Displays an era/epoch string (e.g. A.D.)
h Displays the hour as a number without leading zeros (e.g. 1:15:15 PM) using the 12-hour clock. If this is the only character in a user-defined numeric format, use %h.
hh Displays the hour as a number with leading zeros using the 12-hour clock (e.g. 01:15:15 PM).
H Displays the hour as a number without leading zeros using the 24-hour clock (e.g. 1:15:15). If this is the only character in a user-defined number

format, use %H.
HH displays the hour as a number with leading zeros (e.g. 01:15:15) using the 24-hour clock.
m Display minutes as a number without leading zeros (e.g. 12:1:15). If this is the only

character in a user-defined number format, use %m.
mm Display minutes as a number with leading zeros (e.g. 12:01:15).
s Display seconds as a number without leading zeros (e.g. 12:15:5). If this is the only character

in a user-defined number format, use %s.
ss displays seconds as a number with leading zeros (e.g. 12:15:05).
F displays the fractional part of seconds. For example, ff will be displayed to the nearest hundredth of a second, while ffff will be displayed to the nearest ten thousandth of a second.

Up to seven f symbols can be used in user-defined formats. If this is the only character in a user-defined number format, use %f.
T uses a 12-hour clock and displays a capital A for any hour before noon and a capital P for any hour between noon and 11:59 P.M. If this is the only character in a user-defined number format, use %t.
tt Uses a 12-hour clock and displays uppercase AM for any hour before noon; displays uppercase PM for any hour between noon and 11:59 P.M.
y Displays the year (0-9) as a number without leading zeros. If this is the only character in a user-defined number format, use

%y.
yy Displays the year in two-digit format with leading zeros (if applicable).
yyy displays the year in four-digit format.
yyyy Displays the year in four-digit format.
z displays the time zone offset without leading zeros (e.g. -8). If this is the only character in a user-defined number format, use

%z.
zz Displays the time zone offset with leading zeros (e.g. -08)
zzz Displays the complete time zone offset (e.g. -08:00)

The same formatting can also be done for time. You can use Define the format to set the format output you want.
You can use the characters in the above table for any combination, regardless of the order. For example, if you write yyyy-yyyy, it is also possible.

Note: there are certain requirements for formatting If the date is in DateTime format, the type in SQL is the same. Otherwise, the format is invalid or wrong. Especially in the format of DataGrid {0:IFromat}, it will be used

2) Date input
We are entering the date format DateTime.Pares() is commonly used, but the conversion in this form is quite limited. Some C# will not understand the date format you write, such as 20031231. Everyone knows that it is 2003-

12-31, but C# does not recognize it. We can proceed as follows
//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));
This way he can see the date format of No. 20031231

But it's a pity that he can only convert the yyyyMMdd format and cannot convert other formats. Is it a fly in the ointment?
But. NET provides another overloaded version.
public static DateTime ParseExact(string, string[], IFormatProvider, DateTimeStyles);
can convert several specified date formats.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!