Joda
Joda

Reputation: 12896

Localization of date-time using custom patterns

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.

Upvotes: 1

Views: 8048

Answers (3)

to StackOverflow
to StackOverflow

Reputation: 124726

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

Upvotes: 4

netadictos
netadictos

Reputation: 7722

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.

Upvotes: 2

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

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

Upvotes: -1

Related Questions