Is this a valid German Datetime Culture format?
Asked Answered
D

2

5

So I'm using an API where a datetime is retrieved in the following format DIE, 28. AUG 2018 12:38:38

The closest datetime format I found was this ddd, dd. MMM yyyy HH:mm:ss. It works only if its DI , but the API provides this day as DIE.

Does a format exist that can parse DIE, 28. AUG 2018 12:38:38 I believe that the developers of this API use some different format.

Does anyone know what it could be ?

EDIT: some code I tried

static void Main(string[] args)
{
    const string DateTimeFormat = "ddd, dd. MMM yyyy HH:mm:ss";
    var deCultureInfo = new CultureInfo("de-DE");

    string input = "DIE, 28. AUG 2018 14:12:01";

    // both throws exception
    var timestamp1 = DateTime.ParseExact(input, DateTimeFormat, deCultureInfo);
    var timestamp2 = DateTime.Parse(input, deCultureInfo);


    //Console.WriteLine(DateTime.Now.ToString(deCultureInfo));

}
Disorganization answered 28/8, 2018 at 9:49 Comment(2)
It's interesting that MON, 27. AUG 2018 12:38:38 works. Week days are usually abbreviated with only 2 letters in Germany. And the only 3 letter week day that works with DateTime.Parse is money with Mon.Tutelage
actually it works if CultureInfo is ` CultureInfo.InvariantCulture` , so you are right its a misleading info. I edited it.Disorganization
H
5

Just to offer another option: Just don't.

Whatever that part is, it's redundant. There is no way the 28th of August 2018 will be anything else. The information there has no value to a computer. That format is meant for humans, who cannot just "look that up" in a few CPU cycles.

Don't parse input, just parse input.Split(",").Last().Trim(). This API obviously isn't written by experts or not meant to be used by another computer (German date format, week day, wrong format for weekday). It seems like this is a string that is displayed to end users of a product. Consider it a risk for breaking changes at any time, it's probably whatever user's "like best" at a specific time.

Haller answered 28/8, 2018 at 10:19 Comment(0)
S
9

You can override ddd in the property AbbreviatedDayNames of the used DateTimeFormatInfo:

const string DateTimeFormat = "ddd, dd. MMM yyyy HH:mm:ss";
var deCultureInfo = new CultureInfo("de-DE");

var input = "DIE, 28. AUG 2018 14:12:01";

CultureInfo ci = CultureInfo.CreateSpecificCulture("de-DE");
DateTimeFormatInfo dtfi = ci.DateTimeFormat;

// addjust this to your german strings
dtfi.AbbreviatedDayNames = new[] { "SON", "MON", "DIE", "MIT", "DON", "FRE", "SAM" };

var timestamp1 = DateTime.ParseExact(input, DateTimeFormat, dtfi);

See this from the doku.

Shelves answered 28/8, 2018 at 10:1 Comment(0)
H
5

Just to offer another option: Just don't.

Whatever that part is, it's redundant. There is no way the 28th of August 2018 will be anything else. The information there has no value to a computer. That format is meant for humans, who cannot just "look that up" in a few CPU cycles.

Don't parse input, just parse input.Split(",").Last().Trim(). This API obviously isn't written by experts or not meant to be used by another computer (German date format, week day, wrong format for weekday). It seems like this is a string that is displayed to end users of a product. Consider it a risk for breaking changes at any time, it's probably whatever user's "like best" at a specific time.

Haller answered 28/8, 2018 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.