A little understanding of the date display format string

Source: Internet
Author: User
Tags month name
Http://www.cnblogs.com/longsan/archive/2006/04/05/367462.html

Today, some netizens posted the following code on the Q Group:

Strings=calendar1.todaysdate.tostring ("D");

Want to "mm/dd/yyyy" display way, but always show as "04-01-2006", not Want "04/01/2006".

At first I took it for granted that "MM/DD/YYYY" (custom format string) or "D"
(standard format string), but after experimentation, the discovery always shows "04-01-2006". Later, I looked for help, and some of the following knowledge about the problem:

1. The display of the standard format string is related to the local area on the machine where the program is running, that is, Cultrueinfo. For example, in the local locale on your machine, the spacer between the set date is "-", then you will always show "-" by the above code unless you modify the local locale settings.

2, the above code default American English display way is "04/01/2006", Chinese display Way is "04-01-2006", the German display way is "04.01.2006".

3, if you do not modify the local area settings, and want to implement the "04/01/2006" display mode, you should use a custom format string, the above code can be written as follows:

Strings=calendar1.todaysdate.tostring ("MM '/' dd '/' yyyy");


Two of these single quotes are enclosed in any string and are displayed directly when displayed.

Several solutions:

The first of these is as above 3 says.

The second Kind

Web. config, add:

<globalization requestencoding= "Utf-8" responseencoding= "Utf-8" culture= "ZH-CN"/>

This is the most convenient, but did not try.

Third Kind

Control Panel-〉 Regional Options-〉 Custom (buttons to the right of the time)

Yes, but there is no portability.

For some information reference:

Date formatting display
DateTime in SQL is a 8-bit length date in the specified form of 2003-12-31 00:00:00
The only part of the 2003-12-31 is the most commonly used on ASP. You can use
DateTime.Now.ToString ("D") or DateTime.Now.ToShortDateString ();
But the exact format is 2003/12/31 or 2003-12-31. It depends on your system configuration date configuration.
You want to be sure to get the 2003-12-31 specified words to use. Tostring () user-defined format
The characters in the format can be seen in the document mentioned above.
As. 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 the

%d
DD Displays the day as a number with leading zeros (such as 01).
DDD Displays the day as an abbreviated form (for example, Sun).
DDDD Displays the day as the full name (for example, Sunday).
M Displays the month as a number without leading zeros (for example, January is represented as 1). If this is the only one in the user-defined number format

characters, please use%M.
MM Displays the month as a number with leading zeros (for example, 01/12/01).
MMM Displays the month as an abbreviated form (for example, Jan).
MMMM Displays the month as a full month name (for example, January).
GG Displays the era/era string (for example, A.D.)
H displays the hour as a number without leading zeros (for example, 1:15:15 PM) using a 12-hour system. If this is a user-defined number

Unique characters in the word format, use%h.
HH uses a 12-hour system to display the hour as a number with leading zeros (for example, 01:15:15 PM).
H displays the hour as a number without leading zeros using a 24-hour system (for example, 1:15:15). If this is a user-defined number

The unique characters in the format, use%H.
HH uses a 24-hour system to display the hour as a number with leading zeros (for example, 01:15:15).
M displays the minute as a number without leading zeros (for example, 12:1:15). If this is the only word in a user-defined number format

, use%m.
MM Displays the minute as a number with leading zeros (for example, 12:01:15).
s displays the seconds as a number without leading zeros (for example, 12:15:5). If this is the only character in a user-defined number format

, please use%s.
The SS displays seconds as a number with leading zeros (for example, 12:15:05).
F shows the decimal part of the second. For example, the FF will be accurately displayed to 1% seconds, and the FFFF will be displayed exactly one out of 10,000 seconds.

You can use up to seven F symbols in a user-defined format. If this is the only character in a user-defined number format, use%f.
T uses a 12-hour system and displays uppercase A for any hour before noon, or any small between noon and 11:59 p.m.

The uppercase P is displayed. If this is the only character in a user-defined number format, use%t.
TT uses a 12-hour system and displays uppercase AM for any hour before noon, or any small between noon and 11:59 p.m.

The uppercase PM is displayed.
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 the

%y
YY Displays the year, if applicable, in a two-digit number format with leading zeros.
YYY Displays the year in four-digit number format.
YYYY displays the year in four-digit number format.
Z Displays the time zone offset without leading zeros (for example,-8). If this is the only character in a user-defined number format, use the

%z
ZZ shows the time zone offset with leading zeros (for example-08)
ZZZ Displays the full time zone offset (for example, -08:00)

It is also possible to format the time as well: You can use the custom format to set the format output you want.
You can use any combination of the characters in the previous table, regardless of the order in which you write yyyy-yyyy.

Note that formatting must be date datetime format. The same type is in SQL. Otherwise, the format is invalid or an error occurs. In particular, in the format of the DataGrid often {0:ifromat}

2) input of the date
We are in the Transact-date format Common datetime.pares ()
But this form of conversion is quite limited, and some C # is not going to understand the date format you write as 20031231 everybody knows it's 2003-

Number 12-31 can C # don't know him. We can do this as follows
Set Language country
System.iformatprovider format=new System.Globalization.CultureInfo ("ZH-CN", true);
Specify conversion format
Response.Write (DateTime.ParseExact (this. TextBox1.Text, "YYYYMMDD", format));
So he can see the date format for number No. 20031231.

But it is a pity that he is only able to yyyymmdd the form can not be converted to other formats is not a drawback?
But. NET provides another overloaded version.
public static DateTime ParseExact (String, string[], IFormatProvider, DateTimeStyles);
You can convert several date formats that you specify.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.