简体   繁体   English

C#Convert.ToDateTIme函数是否将日期读为“dd / mm / yyyy”或“mm / dd / yyyy”?

[英]Does the C# Convert.ToDateTIme function read date as “dd/mm/yyyy” or “mm/dd/yyyy”?

Does the C# Convert.ToDateTIme function read date as "dd/mm/yyyy" or "mm/dd/yyyy"? C#Convert.ToDateTIme函数是否将日期读为“dd / mm / yyyy”或“mm / dd / yyyy”?

I have the same application on my local machine which I uploaded to my remote shared server. 我在我的本地计算机上有相同的应用程序,我上传到我的远程共享服务器。 It was working perfectly on my local machine reading "dd/mm/yyyy", but on my remote machine, it seems to read dates as "mm/dd/yyyy". 它完全在我的本地机器上读取“dd / mm / yyyy”,但在我的远程机器上,似乎将日期读作“mm / dd / yyyy”。 I have the same culture setting "en-GB" on both. 我在两者上都有相同的文化设置“en-GB”。

I find this date conversion very unpredictable. 我发现这个日期转换非常不可预测。 Can anyone recommend a culture-proof way of reading date strings from a SQL Server 2005 database? 任何人都可以推荐一种从SQL Server 2005数据库中读取日期字符串的文化证明方法吗?

Well by the sounds of it... One of the settings on the server is off. 听听它的声音......服务器上的一个设置是关闭的。

I'd go through the "Region & Language" Options with a fine tooth comb and make sure that something isn't override but if that fails. 我会用细齿梳子检查“区域和语言”选项,并确保不会覆盖某些东西,但如果失败了。

You could try explicitly setting the Culture Info 您可以尝试明确设置文化信息

         string x = "21/01/2009";

        CultureInfo ci = new CultureInfo("en-GB");

        Convert.ToDateTime(x, ci);

Why not simply use the datetime data type? 为什么不简单地使用datetime数据类型? Then, from your sql data reader, use: reader.GetDateTime(int column) 然后,从您的SQL数据读取器,使用:reader.GetDateTime(int column)

Another option: if you have to pass dates in strings, use the ISO format: yyyy-MM-dd 另一种选择:如果必须在字符串中传递日期,请使用ISO格式:yyyy-MM-dd

How are your dates actually stored in SQL Server? 您的日期如何实际存储在SQL Server中? If they're stored as datetime then you should be able to read them as DateTime values and not need to use Convert.ToDateTime(). 如果它们存储为datetime那么您应该能够将它们作为DateTime值读取,而不需要使用Convert.ToDateTime()。 Can you show us the query and/or the C# code? 你能告诉我们查询和/或C#代码吗?

Edit 编辑

You threw me off with the mention of SQL Server at the end of your question! 你在问题的最后提到了SQL Server,这让你失望了! Have you considered using a DateTimePicker instead of a TextBox? 您是否考虑过使用DateTimePicker而不是TextBox? ASP.NET has date picker controls, right? ASP.NET有日期选择器控件,对吧? Then it could do the parsing for you. 然后它可以为你解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM