简体   繁体   English

将本地化的字符串转换为日期时间

[英]Converting a localized string to datetime

I'd like to convert a localized string (preferably any supported language) to a datetime object. 我想将本地化的字符串(最好是任何支持的语言)转换为datetime对象。 The string is for example (in dutch): woensdag 3 juni 2015 9:12:14 uur CEST 例如,该字符串(在荷兰语中):woensdag 3 juni 2015 9:12:14 uur CEST

The localized string is always of the same format: [day name] [day] [month] [year] [hour] [minute] [second] [literal word for hour] [time zone] The string provided to the program can't be changed on the host application (not enough privileges). 本地化的字符串始终具有相同的格式:[日名称] [日] [月] [年] [小时] [分钟] [秒] [小时的文字] [时区]提供给程序的字符串可以'不能在主机应用程序上更改(权限不足)。

I've read that in C# .NET I need to use something like an InvariantCulture object to change a DateTime object to a localized string date. 我已经读过在C#.NET中,我需要使用类似InvariantCulture对象的方法将DateTime对象更改为本地化的字符串日期。

Is it however possible to go the other way around? 但是,是否有可能走另一条路呢? If so is it possible with my requirements above? 如果可以,上述要求是否可行?

Thanks for any help in advance! 感谢您的任何帮助!

First of all, DateTime is time zone awareness . 首先, DateTime是时区感知 It does not have any information about time zone. 它没有有关时区的任何信息。 That's why you need to parse this CEST part as a literal delimiter. 这就是为什么您需要将此CEST部分解析为文字分隔符的原因。 ( AFAIK , there is no way to parse them other than escape it) Looks like uur means "hour" in english, you need to specify it as a literal as well. AFAIK ,除了对其进行转义以外,没有其他方法可以解析它们)看起来uur在英语中意为“小时”,因此您也需要将其指定为文字。

Then, you can parse it your string with dddd d MMMM yyyy H:mm:ss 'uur CEST'" format and nl-BE culture like; 然后,您可以使用dddd d MMMM yyyy H:mm:ss 'uur CEST'"格式和类似nl-BE文化来解析您的字符串。

string s = "woensdag 3 juni 2015 9:12:14 uur CEST";
DateTime dt;
if(DateTime.TryParseExact(s, "dddd d MMMM yyyy H:mm:ss 'uur CEST'", 
                          CultureInfo.GetCultureInfo("nl-BE"),
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt); // 03/06/2015 09:12:14
}

You definitely not wanna use InvariantCulture to parse this string. 绝对不想使用InvariantCulture来解析此字符串。 This culture is english-based and keep DayNames with their english names as Wednesday etc.. 这种文化是基于英语的,并且将DayNames的英语名称保留为DayNames等。

By the way, Nodatime has ZonedDateTime structure and looks like it supports a time zone with it's Zone property . 顺便说一下,Nodatime具有ZonedDateTime结构,并且看起来像它的Zone属性支持时区。

I've managed to solve the issue thanks to the comments placed underneath the original question that was asked. 多亏了原始问题下方的注释,我设法解决了这个问题。

By using string replacements I can ditch the localized 'uur' which stands for hour in dutch along with the CEST. 通过使用字符串替换,我可以放弃本地化的“ uur”(与CEST一起在荷兰停留一个小时)。

The following code did the trick for me: 以下代码对我有用:

CultureInfo nlculture = new CultureInfo("nl-NL");
string test = "woensdag 3 juni 2015 9:12:14";
DateTime dt = DateTime.ParseExact(test, "dddd d MMMM yyyy H:mm:ss", nlculture);
System.Windows.MessageBox.Show(dt.ToString());

To come back on one of the requirements, which was having it support multiple languages. 回到要求之一,那就是它支持多种语言。 I can (after checking again with the things I can work with) capture the used language, thus letting me able to support multiple languages. 我可以(在再次检查可以使用的东西之后)捕获所使用的语言,从而使我能够支持多种语言。

Thanks all for the help 谢谢大家的帮助

The code below should work. 下面的代码应该工作。 I'm getting an error but I think it is due to an old version of VS installed on my computer 我收到错误消息,但我认为这是由于计算机上安装了旧版VS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;


namespace ConsoleApplication53
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime x = DateTime.ParseExact("3 june 2015 9:12:14 PM GMT", "d MMMM yyyy h:mm:ss tt Z", CultureInfo.InvariantCulture);
            string y = x.ToString("d MMMM yyyy h:mm:ss tt K", CultureInfo.CreateSpecificCulture("nl-NL"));
            string dutchTimeString = "3 juni 2015 9:12:14 uur CEST";

            DateTime date = DateTime.ParseExact(dutchTimeString, "d MMMM yyyy h:mm:ss tt Z", CultureInfo.CreateSpecificCulture("nl-BE"));

        }

    }
}

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

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