Custom Global Application Culture & DateTime.Parse

http://www.codeproject.com/Articles/19762/Custom-Global-Application-Culture

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... :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值