0

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!

Dragon54
  • 313
  • 6
  • 19
  • Have you tried something like DateTime.Parse("donderdag 3 juni 2015 9:12:14 uur CEST", CultureInfo.CurrentCulture): ? That should give you a usable datetime. – Falgantil Oct 21 '15 at 12:23
  • 1
    Possible duplicate of [Parse string to DateTime in C#](http://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp) – Liam Oct 21 '15 at 12:24
  • @Liam Partially true, although OP asks specifically in relation to language-specific representations, whereas the post you reference is for a general date-time structure. – Falgantil Oct 21 '15 at 12:25
  • The duplicate is more because this doesn't show any research effort. The OP should be able to easily take the dupe question and try themselves. Then if they have a **specific question** (which this is not) then they can ask that – Liam Oct 21 '15 at 12:27
  • Very true, point taken. I rest my case. – Falgantil Oct 21 '15 at 12:27
  • 1
    You _can't_ parse this string because it is **not** a valid `DateTime` in Gregorian Calendar. [June 3th 2015](http://www.calendar-365.com/calendar/2015/June.html) is Wednesday, not Thursday (which is "donderdag" in Dutch) – Soner Gönül Oct 21 '15 at 12:29
  • 1
    The invariant culture is the exact *opposite* of what you want here. You need the right format string, with the right culture, which certainly *isn't* the invariant culture. – Jon Skeet Oct 21 '15 at 12:29
  • and you have no information about the culture ? So the date could be parsed specific to that culture ? – Kapoor Oct 21 '15 at 12:56
  • You can parse the string with appropriate CultureInfo, it supports localized format. – krafty Oct 21 '15 at 13:01
  • @SonerGönül that is indeed correct, my mistake. It has been edited. – Dragon54 Oct 21 '15 at 13:09

3 Answers3

1

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

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

Dragon54
  • 313
  • 6
  • 19
0

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

        }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • First, this does not answer the question. Your string does not have any AM/PM designator but you use `tt` in your `ParseExact` method. That does not make sense. And second, your string does not even the same with OP. Third, using `Z` with `DateTime` parsing is not recommended. It will be better to use it with `DateTimeOffset` instead. – Soner Gönül Oct 21 '15 at 13:45
  • uur is AM/PM designator. CEST is GMT which is offset zero. If you used google translation you would of found this out. – jdweng Oct 21 '15 at 13:49
  • No. [`uur` means](https://en.wiktionary.org/wiki/uur) `hours` in Dutch. It is not a designator or something. `nl-BE` culture has an empty strings for them (at least in .NET 4 and Win7). And your string is _still_ not the same with OP's. `CEST` offset is not relevant because `DateTime` structure does not support _any_ time zone information. – Soner Gönül Oct 21 '15 at 14:08
  • If I go to google translation and include uur and then remove uur the PM translation comes and goes. it may translate to hours, but it seems the meaning is actually PM. I think the real reason for this posting is the fact that the Net Library doesn't properly support the AM/PM (tt) in Dutch. – jdweng Oct 21 '15 at 16:29