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.