简体   繁体   English

C#日期格式

[英]C# date formatting

This is a problem I have come across a number of times and I'm sure there's an elegant solution I'm missing. 这是我遇到过很多次的问题,并且我确定我缺少一个优雅的解决方案。

I have some DateTime variables in c# that I get sent from various SQL tables/websites/web services, often sent as strings. 我在c#中有一些DateTime变量,这些变量是从各种SQL表/网站/网络服务发送的,通常以字符串形式发送。 The problem is that some of these sources are set up as English (United States) and some are set up as English (British). 问题在于,其中某些来源设置为英语(美国),而另一些来源设置为英语(英国)。 I don't have control over some of these, as I would dearly like to set them all to one culture or the other. 我无法控制其中的一些,因为我非常想将它们全部设置为一种文化或另一种文化。

Until now I have been converting these using a CultureInfo object to format it correctly like: 到目前为止,我一直在使用CultureInfo对象将其转换为正确的格式,例如:

CultureInfo ci = new CultureInfo("en-GB");
Convert.ToDateTime(inputDateTimeString, ci);

However, it has only recently occured to me that the conversion doesn't know what culture the original DateTime is in (as I said, it could be either American or British) since it is just a string. 但是,直到最近,我才发现转换不知道原始DateTime所处的文化(正如我所说,它可能是美国或英国),因为它只是一个字符串。

In the case of a date string, say "06/15/2009", this is fine because the conversion recognises that '15' can't be the month. 如果使用日期字符串,请说“ 06/15/2009”,这很好,因为转换识别出“ 15”不能是月份。 However a date string of "06/07/2009" will always be valid but, depending on whether the original was American or British, it could be referring to different days and months. 但是,日期字符串“ 06/07/2009”将始终有效,但是取决于原始文件是美国还是英国,它可能是指不同的日期和月份。

Is there a better accepted method for handling DateTime's in general that cuts down on these ambiguities? 有没有一种更好的公认的方法来处理DateTime,从而减少这些歧义? Thanks. 谢谢。

EDIT: Right, so it seems there is no reliable way to always convert into the correct format because my information is limited. 编辑:对,所以似乎没有可靠的方法来始终转换为正确的格式,因为我的信息有限。

One source of these DateTime strings is a .dll I have no control over. 这些DateTime字符串的一个来源是我无法控制的.dll。 However, I DO have control over the SQL login that this .dll uses to access the database where the information is stored. 但是,我确实可以控制该.dll用于访问存储信息的数据库的SQL登录名。 If I were to change the language setting of this login to British English (it is currently American English), would it retrieve DateTime's in that format, or would it have no effect? 如果我要将登录名的语言设置更改为英式英语(当前为美式英语),它将以该格式检索DateTime还是无效? I'd have to check ofcourse that it didn't screw anything else up, but might it work? 当然,我不得不检查它没有弄乱其他任何东西,但是它可以工作吗?

You need to tackle the problem at its source: you're being given data in an ambiguous format. 您需要从根本上解决问题:数据格式不明确。 If you can't change the data sources themselves, you should decorate them with a format string or something like that, so that you know to treat the whole data source in one way or the other. 如果您不能自己更改数据源,则应使用格式字符串或类似的格式来修饰它们,以便您知道以一种或另一种方式处理整个数据源。

Without that information you simply can't reliably parse the data. 没有这些信息,您将无法可靠地解析数据。

What I usually do (with any parsing) is: 我通常所做的(使用任何解析)是:

  1. TryParse using the culture of the user TryParse使用用户的文化
  2. If it fails, Parse using the culture invariant flag 如果失败,则使用区域性不变标志进行解析

If I understand your question correctly it is a bit out of your control - if you get these dates from external sources, you need to know what format they are in. Once you get a DateTime object in C# the initial format doesn't matter - it just contains the date and time regardless of the format. 如果我正确地理解了您的问题,那将有点超出您的控制范围-如果您从外部来源获得这些日期,则需要知道它们的格式。一旦获得了C#中的DateTime对象,初始格式就没有关系了-它仅包含日期和时间,与格式无关。

On the other hand why don't you get them as some sort of date time database format from the database? 另一方面,为什么不从数据库中获取它们作为某种日期时间数据库格式呢?

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

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