JeremyBeadle
JeremyBeadle

Reputation: 693

C# DateTime parsing international cultures

I'm trying to parse a variety of date formats, the day number, time and year components are consistent but the day of the week and month are device dependent. These are some example results, coming from iPads

Isn Mac 11 16:25:58 2013

Ahd Mac 10 18:34:41 2013

Sab Mac 09 18:44:34 2013

Jum Mac 08 16:57:33 2013


Fri Mar 8 16:49:46 2013

Thu Mar 7 12:34:47 2013

Sat Jan 19 12:21:58 2013

The English ones parse fine but the Malaysian (Malay) ones never do

I was using this to debug it:

        var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

        foreach (var culture in allCultures)
        {
            if (DateTime.TryParseExact("Isn Mac 11 16:25:58 2013", "ddd MMM d HH:mm:ss yyyy", culture, DateTimeStyles.None, out result))
                return;
        }

Ideally it would handle all possible languages in this format and I wouldn't have to write language specific code

I've tried manually parsing the Malaysian dates with the following culture codes: ms ms-BN ms-MY

Any ideas?

Thanks for your time

Upvotes: 3

Views: 1744

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460288

I have tested your datetime with the malaysian culture. Obviously your string has the incorrect day name:

var dt = new DateTime(2013,03,11,16,25,58);
var currCult = CultureInfo.CreateSpecificCulture("ms-MY");
var str = dt.ToString("ddd MMM d HH:mm:ss yyy", currCult);
// ---> "Isnin Mac 11 16:25:58 2013"

Imho Isn != Isnin,

Upvotes: 1

Oded
Oded

Reputation: 499352

This may be a bug in the CultureInfo data for Malasian cultures - the AbbreviatedDayNames array looks like this:

Ahad 
Isnin 
Sel 
Rabu 
Khamis 
Jumaat 
Sabtu 

You will not that Isn doesn't exists as such - it is Isnin.

For comparison, look at DayNames:

Ahad 
Isnin 
Selasa 
Rabu 
Khamis 
Jumaat 
Sabtu 

I had picked these out from:

System.Globalization.CultureInfo.GetCultureInfo("ms-MY").DateTimeFormat

As a solution, see if you can use full day names - using Isnin instead of Isn in your example, returns the correct DateTime object.

Upvotes: 2

Related Questions