0

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?

Phill Greggan
  • 2,234
  • 3
  • 20
  • 33
  • 3
    You can set the culture in the `web.config` file (but not clear what your mean by _sometimes_ - do you want it to be different for different controller methods?) –  Dec 16 '15 at 02:36
  • @StephenMuecke so i assume that if different date formats in different controller then manual formatting but if one format the whole application it would be culture in web.config? – Phill Greggan Dec 16 '15 at 02:38
  • @StephenMuecke if you could post how to set that appreciate it, when i set this culture does it effect all the other configurations such as currency and time zone, because it should not only the time format needs the change – Phill Greggan Dec 16 '15 at 02:41
  • 1
    Yes, if your wanted to set the server culture you can use (say) `` (Australia). If you want different formats _sometimes_, then you will have to set the culture in the controller. –  Dec 16 '15 at 02:41
  • @StephenMuecke but does this effect the other setting such as currency and time zone etc? – Phill Greggan Dec 16 '15 at 02:42
  • Sorry, was not clear on your last comment - are you saying that you want only the date format to change (setting the culture will also affect currency etc) –  Dec 16 '15 at 02:42
  • @StephenMuecke yes only date needs the change and nothing else : ) – Phill Greggan Dec 16 '15 at 02:43
  • In that case, you would need to set the `DateTimeFormat` property of the current culture (probably in `Global.asax`). –  Dec 16 '15 at 02:45
  • @StephenMuecke if you could post how to set that i could give you points : ) – Phill Greggan Dec 16 '15 at 02:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98046/discussion-between-stephen-muecke-and-phill-greggan). –  Dec 16 '15 at 02:49

2 Answers2

1

Try setting the culture in Web.config

<globalization culture="en-GB"/>
chamara
  • 12,649
  • 32
  • 134
  • 210
1

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.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212