Phill Greggan
Phill Greggan

Reputation: 2404

Is there a way to set an application level datetime format?

C# / MVC generates datetime in the format MM/DD/YYYY but sometimes i need to put in the format DD/MM/YYYY. This is done through giving a custom format. But instead of writing code to make C# generate datetime as DD/MM/YYYY is there an application level configuration i could make such as a change in web.config, app.config or machine.config?

Upvotes: 0

Views: 3024

Answers (2)

NightOwl888
NightOwl888

Reputation: 56909

If all you are trying to do is set the date format application-wide, you can set the Culture in the Web.config

<globalization culture="en-GB" />

However, if you are trying to reduce repetitive code, you could change the culture of the current thread and then switch it back immediately after you are finished calling date.ToShortDateString(). Here is a CultureContext class that will change the format only inside of the using block.

/// <summary>
/// Allows switching the current thread to a new culture in a using block that will automatically 
/// return the culture to its previous state upon completion.
/// </summary>
public class CultureContext
    : IDisposable
{
    public CultureContext(
        CultureInfo culture,
        CultureInfo uiCulture
        )
    {
        if (culture == null)
            throw new ArgumentNullException("culture");
        if (uiCulture == null)
            throw new ArgumentNullException("uiCulture");

        this.currentThread = Thread.CurrentThread;

        // Record the current culture settings so they can be restored later.
        this.originalCulture = this.currentThread.CurrentCulture;
        this.originalUICulture = this.currentThread.CurrentUICulture;

        // Set both the culture and UI culture for this context.
        this.currentThread.CurrentCulture = culture;
        this.currentThread.CurrentUICulture = uiCulture;
    }

    private readonly Thread currentThread;
    private readonly CultureInfo originalCulture;
    private readonly CultureInfo originalUICulture;

    public CultureInfo OriginalCulture
    {
        get { return this.originalCulture; }
    }

    public CultureInfo OriginalUICulture
    {
        get { return this.originalUICulture; }
    }

    public void Dispose()
    {
        // Restore the culture to the way it was before the constructor was called.
        this.currentThread.CurrentCulture = this.originalCulture;
        this.currentThread.CurrentUICulture = this.originalUICulture;
    }
}

Usage

// Temporarily override the current thread's culture with the invariant culture
using (var cultureContext = new CultureContext(
    CultureInfo.InvariantCulture,
    System.Threading.Thread.CurrentThread.CurrentUICulture))
{
    // All date formats here will be in the invariant culture

    using (var originalCultureContext = new CultureContext(
        cultureContext.OriginalCulture, 
        cultureContext.OriginalUICulture))
    {
        // All date formats here will be in the default culture
    }

    // All date formats here will be in the invariant culture

}

See this answer to understand the difference between Culture and UI Culture.

Upvotes: 1

chamara
chamara

Reputation: 12709

Try setting the culture in Web.config

<globalization culture="en-GB"/>

Upvotes: 1

Related Questions