Click here to Skip to main content
Licence CPOL
First Posted 26 Jul 2007
Views 19,106
Bookmarked 12 times

Custom Global Application Culture

By Mahendra Kumar Srivastava | 26 Jul 2007
This article helps you in defining the application specific culture at the startup time of your application.

1
1 vote, 16.7%
2

3
1 vote, 16.7%
4
4 votes, 66.7%
5
3.83/5 - 6 votes
μ 3.83, σa 2.12 [?]

Introduction

Many of the times, developers code their application and forget that different systems can have different date and time format settings. This causes the application to crash. This article helps you in setting the Custom global culture info environment specific to your application.

Background

I was working on an existing application and found out the carelessness of the developers. If I changed my system settings, I could not run this application - this issue was assigned to me for fixing. So I did in this way.

Using the Code

The code is self explanatory. Now developers need not worry about the environment settings for the time and date formats.

Set and initialize all the Culture Settings at the start of your application:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    // Creating a Global culture specific to our application.
    System.Globalization.CultureInfo cultureInfo = 
		new System.Globalization.CultureInfo("en-US");
    // Creating the DateTime Information specific to our application.
    System.Globalization.DateTimeFormatInfo dateTimeInfo = 
		new System.Globalization.DateTimeFormatInfo();
    // Defining various date and time formats.
    dateTimeInfo.DateSeparator = "/";
    dateTimeInfo.LongDatePattern = "dd-MMM-yyyy";
    dateTimeInfo.ShortDatePattern = "dd-MMM-yy";
    dateTimeInfo.LongTimePattern = "hh:mm:ss tt";
    dateTimeInfo.ShortTimePattern = "hh:mm tt";
    // Setting application wide date time format.
    cultureInfo.DateTimeFormat = dateTimeInfo;
    // Assigning our custom Culture to the application.
    Application.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;

    ..................
    Application.Run(new Form1());
}

Points of Interest

Well most of you have tried the same thing but maybe it doesn't allow you to change the DateFormats directly. I tried to change the CurrentCulture.DateFormatInfo at various times, but... :)

History

  • 26th July, 2007: Initial post

License

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

About the Author

Mahendra Kumar Srivastava

Software Developer (Senior)
Misys
India India

Member
Started carreer with CEERI, Delhi, India as a C Programmer in 2004 and migrated my learning experiance to C#.Net in 2005 with DIGISIGN Noida, Joined Bally in 2006, worked with JK Technosoft since Jun 2007 till Apr 2009, joined back Bally in June 2009 and joined Misys in June 2010. Worked in different domains such as Casino Management, Digital Signage, and Medical and currently working with Misys in Banking domain.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalgood PinmemberPL011:26 21 Apr '09  
GeneralRe: good PinmemberMahendra Kumar Srivastava1:18 17 Jul '09  
Generalexcellent Pinmemberanand.lv23:23 30 Nov '08  
GeneralRe: excellent PinmemberMahendra Kumar Srivastava1:19 17 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 26 Jul 2007
Article Copyright 2007 by Mahendra Kumar Srivastava
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid