Search code examples
c#.netdatetimedateglobalization

Localizing Date Ranges


Does anyone know how to localize date ranges using C#?

In particular, I want to generate "smart" date ranges, so that redundant information is eliminated.

Here are some examples in US English

  1. August - Sept, 2009
  2. August 2009
  3. August 1 - 9, 2009
  4. January 1 - March 3, 2009
  5. December 6, 2009 - January 8, 2010

Form what I can tell the .NET Framework has support for localizing dates, but not ranges of dates.

Using the information in System.Globalization.DateTimeFormatInfo for the Windows locales that CultureInfo supports, I was able to figure out (most of the time) how to do items #1 and #2. Item #2 is just DateTime.ToString(DateTimeFormatInfo.YearMonthFormat). Using YearMonthFormat I was also able to deduce formats to use for #1 for most languages. For the few that I couldn't I just duplicate the year.

Unfortunately, I can't figure out how to do items #3-#5 using the .NET Framework. Outlook formats ranges using those formats, so I was hoping there might be some Win32 APIs that would do it, but a Google search for "Win32 Date Range localization" yielded nothing useful.

I like the enhanced usability provided by "smart range formating", and I would like my customers that are not using English versions of Windows to get the same benefits.

Does any one know how to do that in a culture-dependent manner?


Solution

  • I ended up defining my own range formats for this.

    For the most part I derived them from the LongDatePattern. Where possible I verified the formats against Google Calendar, and / or native speakers.

    I also generated 2 formats for time ranges (within the same day), one for within the same 12 hour period and another for times outside the same 12 hour period (for cultures that use 24 hour time these 2 formats are the same). Those are mostly based on the FullDateTimePattern.

    In all cases I removed "day names" (monday, tuesday, etc) from all the formats where ever possible.

    I would love to post them here, but it seems that the Stack Overflow folks have an irrational fear of html tables, because they don't allow table tags. The data is essentially a giant table. I have no interest in trying to simulate tables using CSS, so I just put the table up on my own site. You can access it here if you are interested:

    http://www.transactor.com/misc/ranges.html

    I do have some detailed notes on how I derived a lot of the formats, including what's been validated and what hasn't, if you are really interested. If you want those notes, just send an email to:

    [email protected]

    and I'd be happy to send them to you.