C# – ASP.NET C# MVC – Change format for datetime directly on DB or Model

asp.net-mvcc++datetime

I'm struggling myself trying to find an easy way to change the DateTime format for my Table field.

I have a Model called Article with a field called releaseDate that is a DateTime

I managed to do it (visually) by converting the

Article.releaseDate.ToString("dd/MM/yy")

but the thing is when I try to submit a date with this format from the create action it returns an error telling the the format is wrong.

Any easy way to change the default ("MM/dd/yy") to ("dd/MM/yy")?

Thanks in advance

Best Solution

You could change the culture information on a page by page basis by including

<%@ Page UICulture="en" Culture="en-GB" %>

or globally across all pages by adding to your web.config

<globalization uiCulture="en" culture="en-GB" />

Both will change the DateTime model binding to dd/MM/yyyy so no conversion is necessary.

See this question for more information

The code equivalent is

CultureInfo.CurrentUICulture.DateTimeFormat
 = CultureInfo.CurrentCulture.DateTimeFormat
 = new CultureInfo( "en-GB", false ).DateTimeFormat;