Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Croatian Date string into DateTime

I have the following string from WebService "1.4.2013. 0:00:00". I want to get DateTime object from that string. What I have tried so far:

string d = "1.4.2013. 00:00:00";
 DateTime parsed = DateTime.ParseExact(d, "d",CultureInfo.CreateSpecificCulture("hr-HR"));

DateTime parsed = DateTime.ParseExact(d, "d",new CultureInfo("hr-HR"));
DateTime parsed = DateTime.ParseExact(d, "d", CultureInfo.InvariantCulture);

It tells me that

string is not recognized as valid dateTime string.

I want to solve this without string parsing eg: removing the dot after the year.

like image 299
Riste Avatar asked Sep 10 '26 06:09

Riste


1 Answers

The "d" standard format specifier uses ShortDatePattern of supplied culture. Since you use DateTime.ParseExact, format and string should match exactly.

Buthr-HR culture's ShortDatePattern pattern is d.M.yyyy. and this clearly doesn't match with your string. It doesn't match with InvariantCulture either.

However, this format is a standard date and time format for hr-HR culture, so you can use DateTime.Parse directly like;

string d = "1.4.2013. 00:00:00";
DateTime parsed = DateTime.Parse(d, CultureInfo.GetCultureInfo("hr-HR"));
// 01/04/2013 00:00:00

Your string matches The "G" standard format specifier of hr-HR culture which based on combination of ShortDatePattern and LongTimePattern properties as d.M.yyyy. H:mm:ss.

like image 76
Soner Gönül Avatar answered Sep 12 '26 18:09

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!