DateTime.ToString format problem

Source: Internet
Author: User

These days the system found that the format of DateTime.ToString () is somewhat inconsistent with the format that SQL Server can eat. An error occurred that caused the database to not acknowledge DateTime. Check out MSDN, I have some experience with the format of DateTime. Here's the thing.

Because of bad writing. Just use the 123 list.

1.DateTime defaults to the server's current locale format (that is, the current server's culture culture, in fact, the number of ToString is similar).

Date1

This.lblDate.Text = string. Format ("The current default culture is: {0} <br/>", System.Threading.Thread.CurrentThread.CurrentCulture.ToString ());
This.lblDate.Text = = String. Format ("Default:<br/> {0}", DateTime.Now.ToString ());
This.lblDate.Text = = String. Format ("<br/> {0}", DateTime.Now.ToShortDateString ());
This.lblDate.Text = = String. Format ("<br/> {0} <br/><br/>", DateTime.Now.ToLongDateString ());

If we enter the above code, it will appear on my computer as follows:

date1r

The current default culture is: ZH-CN

Default:

2008-12-12 11:30:47

2008-12-12

Ken's Calendar December 12, 2008

See the last line of "Ken's Calendar"? Because I changed "Longdateformat" to "Ken's Calendar ' yyyy ' year ' M ' d ' Day" in the control Panel's locale.

2. If you want to use ToString ("dd-mm-yyyy") without changing the culture of the current thread Customize the format. It is best to add System.Globalization.DateTimeFormatInfo.InvariantInfo. If not added, may also be affected by the current culture. For example:

Date2

This.lblDate.Text = = String. Format ("{0} <br/>", DateTime.Now.ToString ("dd/mm/yyyy"));
This.lblDate.Text = = String. Format ("{0} <br/><br/>", DateTime.Now.ToString ("dd/mm/yyyy"), System.Globalization.DateTimeFormatInfo.InvariantInfo));

The result:

Date2r

12-12-2008
12/12/2008

The reason is that under the current culture (ZH-CN), the datetime dateseparator is-rather than/. Plus System.Globalization.DateTimeFormatInfo.InvariantInfo, The system will be output in exactly the custom format.

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.