Reputation: 3685
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));
}
Upvotes: 5
Views: 8703
Reputation: 77304
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.
Upvotes: 5
Reputation: 1671
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.
Upvotes: 9