Dragon54
Dragon54

Reputation: 313

Converting a localized string to datetime

I'd like to convert a localized string (preferably any supported language) to a datetime object. The string is for example (in dutch): 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.

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!

Upvotes: 0

Views: 1942

Answers (3)

jdweng
jdweng

Reputation: 34419

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

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"));

        }

    }
}

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

First of all, DateTime is time zone awareness. It does not have any information about time zone. That's why you need to parse this CEST part as a literal delimiter. (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.

Then, you can parse it your string with dddd d MMMM yyyy H:mm:ss 'uur CEST'" format and nl-BE culture like;

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. This culture is english-based and keep DayNames with their english names as Wednesday etc..

By the way, Nodatime has ZonedDateTime structure and looks like it supports a time zone with it's Zone property.

Upvotes: 1

Dragon54
Dragon54

Reputation: 313

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.

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

Upvotes: 0

Related Questions