简体   繁体   English

C#解析区域日期时间字符串

[英]C# parse regional date time strings

I am trying to make a small application for a project in which clients from across the world can use. 我正在尝试为一个来自世界各地的客户可以使用的项目制作一个小应用程序。 The problem I am having is that the back-end is setup using XML files that gets generated and uploaded to a shared network and in these XML files I store a date time variable that says when a particular task started and finished. 我遇到的问题是后端是使用生成并上传到共享网络的XML文件设置的,在这些XML文件中我存储了一个日期时间变量,表示特定任务何时开始和结束。 The program crashes at certain load intervals with a format exception and after some research I found out that I need to setup a regional date time parser. 程序在某些加载间隔崩溃,格式异常,经过一些研究后我发现我需要设置一个区域日期时间解析器。

The question that I have is how can I convert the time in the XML file, whether it's using US, AU, or UK regional settings, and have it use the client's local settings. 我的问题是如何在XML文件中转换时间,无论是使用美国,澳大利亚还是英国的区域设置,并让它使用客户端的本地设置。 This is being done using VS C# 2005 and the .NET 3.5 framework. 这是使用VS C#2005和.NET 3.5框架完成的。

Thanks in advance for your help. 在此先感谢您的帮助。

The backends should be writing in a standard format. 后端应该以标准格式书写。 The XML shouldn't just use the local date format - that way madness lies. XML不应该只使用本地日期格式 - 这就是疯狂所在。 The main point of XML is to be an interchange format for computers , not for humans . XML的要点是成为计算机的交换格式,而不是人类的交换格式。 Unless you're formatting something for human consumption, you shouldn't be using local variation. 除非您为人类消费格式化某些内容,否则不应使用本地变体。

So either use a specific format everywhere, specifying CultureInfo.InvariantCulture when formatting, or use dt.ToString("o") or another of the culture-invariant format strings. 因此,要么在任何地方使用特定格式,在格式化时指定CultureInfo.InvariantCulture,要么使用dt.ToString("o")或另一种culture-invariant格式字符串。

If you're just talking about a point in time (ie the local time doesn't matter, just the global instant) you should also make sure you use a consistent time zone, where UTC is the obvious choice. 如果您只是在谈论一个时间点(即本地时间无关紧要,只是全球瞬间),您应该确保使用一致的时区,其中UTC是显而易见的选择。 For example, use DateTime.UtcNow instead of DateTime.Now . 例如,使用DateTime.UtcNow而不是DateTime.Now

Once you've got the generation code doing sensible things, consuming it should be very straightforward. 一旦你已经得到了一代代码做明智的事情, 消费应该是非常简单的。

If, however, you don't have the luxury of changing the generation code, you may have to resort to passing the right CultureInfo into DateTime.TryParseExact . 但是,如果您没有更改生成代码的奢侈,则可能不得不求助于将正确的CultureInfo传递给DateTime.TryParseExact However, this means that you'll need to know the culture used to create the file. 但是,这意味着您需要知道用于创建文件的文化。 If the files are being generated in a culture-sensitive way and you don't know which culture they're using, you're basically stuffed. 如果文件是以文化敏感的方式生成的,并且您不知道他们正在使用哪种文化,那么您基本上就会被填充。 For example, 03/05/2010 could mean March 5th 2010 or May 3rd 2010. Without any more information, you have no way of deciding which to use. 例如,03/05/2010可能意味着2010年3月5日或2010年5月3日。如果没有更多信息,您无法决定使用哪个。

Regional settings implies format. 区域设置意味着格式。 This is not your main concern. 不是你主要担心的问题。 Format is simple (use a DateTime.Parse or .ParseExact . Time zones should be your primary concern . 格式很简单(使用DateTime.Parse.ParseExact时区应该是您主要关注的问题

Please refer to How to convert string to local Date Time? 请参阅如何将字符串转换为本地日期时间?

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

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