Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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?.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading