Click here to Skip to main content
15,887,477 members
Articles / Web Development / IIS
Tip/Trick

Override Date, Time, Currency and Number Formats in ASP.NET Applications

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Jan 2024CPOL2 min read 4.2K   35   1   4
Step-by-step guide to enable an ASP.NET web application to override date, time, currency and number formats.
Use an IIS module to override the server date, time, currency and number formats (dictated by the regional settings) without editing application code.

Introduction

ASP.NET supports globalization in the web.config file, forcing the application to a specific culture, i.e., en-US, en-FR, en-ZA, etc.

For example:

XML
<system.web>  
  <globalization culture="en-ZA"/>
</system.web>

The application then uses the regional settings of the host for displaying currency, number and date time information.

Issues can arise with how currency, number and date/time information is handled if these regional settings differ between hosts or operating systems (e.g.: migrating sites/services from one server to another).

It is sometimes not possible to change the hosts regional settings without affecting other sites and services running on the server.

Background

Recently, I was involved in a project that included migrating ASP.NET sites and services from a server that had reached end of life and was being decommissioned. It seemed fairly straight-forward until the testers reported that the number formats were incorrect for our specific locale (en-ZA).

We were unable to change the number formats on the server without affecting other sites and services so I developed and implemented an IIS module to intercept the request and set the locale and specific formats via the config file.

Features

  • Granular control of currency, number and date/time formats for multiple cultures.
  • Global override (apply specific format regardless of culture).
  • Multi-process support (worker threads also honour specified formats).
  • Override values of all public properties of the NumberFormatInfo and DateTimeFormatInfo objects.
  • No code changes required - all done via the web.config file.

Using the Code

Download Localize.zip (or clone source code from here).

Add the Localize.dll file to the bin directory of your ASP.NET web application or web service.

Next, register the module with IIS by adding the module to the system.webServer section of the web.config file:

XML
<!-- Required -->
<system.webServer>
    <modules>
        <add name="Localize" type="Localize.CultureHelper" />            
    </modules>        
</system.webServer>

Add the settings/properties you wish to override in the appSettings section of the web.config file, e.g.:

XML
<!-- Force a specific culture (optional) -->
<add key="culture.cultureName" value="en-FR" />
    
<!-- Global override (optional, but if specified, all cultures will use these values) -->
<add key="culture..NumberDecimalSeparator" value="!" />
<add key="culture..NumberGroupSizes" value="2,3,2" />
<add key="culture..CurrencySymbol" value="Z" />

<!-- Override culture-specific settings (optional, case-insensitive) -->
<add key="culture.en-za.NumberDecimalSeparator" value="." />
<add key="culture.en-za.NumberDecimalDigits" value="9" />
<add key="culture.en-za.currencySymbol" value="#" />
<add key="culture.en-za.ShortDatePattern" value="MM-dd-yyyy" />
<add key="culture.en-za.ShortTimePattern" value="HHmm" />
<add key="culture.en-za.MonthDayPattern" value="MMM d" />
    
<add key="culture.en-US.currencySymbol" value="#" />
   
<add key="culture.en-fr.currencySymbol" value="@" />

The module uses reflection to match the properties to override. The property names are case-insensitive.

Note: In the config example above, I've used (mostly) ridiculous values to test with, so please change/remove these to suit your needs.

Exceptions are handled gracefully and logged to the Windows Event Log.

Points of Interest

Conclusion

Thanks for reading - I hope this saves somebody some time and frustration.

History

  • 17th January, 2024: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
I'm a software developer that enjoys a great many things. Developing and integrating with API's is one of them. I live on a smallholding in the beautiful KZN midlands in South Africa with my lovely wife, 2 dogs, some chickens, a few sheep, a cat with half a tail, and two feral children.
I may be the world's shoddiest example of an "IT guy": I love being outdoors, exercising, gardening, "farming" (I really cannot call myself a farmer - keeping some animals alive does not make one a farmer), riding motorbikes and generally doing things that I am far too old for.

Comments and Discussions

 
QuestionWhere in the web.config file do the override settings go? Pin
John Gleeson18-Jan-24 5:10
John Gleeson18-Jan-24 5:10 
AnswerRe: Where in the web.config file do the override settings go? Pin
Lance Ford18-Jan-24 5:14
Lance Ford18-Jan-24 5:14 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA18-Jan-24 3:10
professionalȘtefan-Mihai MOGA18-Jan-24 3:10 
QuestionI was involved in a project that included migrating ASP. Pin
bruce zubaa17-Jan-24 1:11
bruce zubaa17-Jan-24 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.