views:

1017

answers:

3

I am working on localization for an app where custom patterns are used to format the date-time.

one example is: dd-MM HH:mm

I need to get localized versions of this custom format for dates, so that I get the date using numbers, and the time, basically using the local order (dd MM or MM dd) and the local seperator for both date and time.

This is fairly trivial, as long as I am using the default formatting, but as soon as I stray from these, the formatting becomes hardcoded.

Any ideas?

Thanks, Jonas

edit: I have the cultureInfo objects, the problem is that when I do a DateTime.ToString("ES-es"), I get too much info - I need only month+day, but with the default ToString, I get year+month+day

Edit again: I see how I can change the ShortDate pattern for each CultureInfo object I use. However, I also need the default ShortDate pattern in some situations, so changing that would, unfortunately, leave me with another, equivalent problem.

Final edit: in case anyone cares. I never did find a solution, so I ended up coding a static function that checks the current CultureInfo, and returns the correctly formatted date, sans year.

A: 

The CultureInfo class would be a good place to start looking.

David Kemp
+1  A: 

Perhaps you could try this:

DateTime.Now.ToString(new System.Globalization.CultureInfo(Thread.CurrentThread.CurrentCulture.Name));

If i want for example to display the time for a particular culture, i would do this:

DateTime.Now.ToString(new System.Globalization.CultureInfo("ES-es"))

The cultureinfo acts as the IFormatProvider.

netadictos
+2  A: 

Look at the DateTimeFormatInfo class (CultureInfo.DateTimeFormat property), in particular the properties DateSeparator, TimeSeparator, ShortDatePattern.

Joe