Datetime localization using custom templates

I am working on localizing an application where custom templates are used to format datetime.

one example: 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 times, basically using a local ordering (dd MM or MM dd) and a local separator for date and time.

It's pretty trivial if I use the default formatting, but as soon as I deviate from them, the formatting becomes hardcoded.

Any ideas?

Thanks, Jonas

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

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

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

+1


source to share


3 answers


Take a look at the DateTimeFormatInfo class (CultureInfo.DateTimeFormat property), specifically the DateSeparator, TimeSeparator, ShortDatePattern properties.



+4


source


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 specific culture, I would do the following:



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

      

Cultureinfo acts like an IFormatProvider.

+2


source


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

-1


source







All Articles