Home > Software design >  Is there a way to exclude a character from the date string while using DateTime.ParseExact?
Is there a way to exclude a character from the date string while using DateTime.ParseExact?

Time:08-16

I have a bunch of strings with dates in Dutch that look like this:

string date =  "5 juli 2011 om 09:29";

I'm trying to parse it like this:

string format = "dd MMMM yyyy HH:mm";
CultureInfo provider = new CultureInfo("nl-NL");
var PublishedOn = DateTime.ParseExact(date, format, provider);

It doesn't work because of the word "om" (which means "at" in Dutch).

I can't use a method to split string before parsing or something like that, because the code is a part of a larger scraper that takes a format string from config, so it should stay generic. I can't put a method or some other logic that manipulates the string in the scraper code because it won't work for other websites anymore. I should be able to just provide a format string and parse the date with it.

Is there a way to exclude the "om" from the date string with format? Maybe with some regex expression in the format or something like that?

I couldn't find anything.

UPDATE: solution is just to use:

string format = "dd MMMM yyyy 'om' HH:mm";

CodePudding user response:

If you want text to be taken literally in a string-to-be-parsed, enclose it in single quotes:

string format = "dd MMMM yyyy 'om' HH:mm";

See also What does a single quote inside a C# date time format mean?.

  •  Tags:  
  • c#
  • Related