2

I have my ViewModel on which I've defined my properties; one of them is a DateTime field defined as:

    [DisplayName("Data")]
    [DataType(DataType.Date)]        
    [Required(ErrorMessage="Selezionare una data")]
    [DisplayFormat(ApplyFormatInEditMode = true,  DataFormatString = "{0:dd/MM/yyyy}" )]
    public DateTime? DtNews { get;set; }

When I submit the form I always receive the error msg "The value '13/07/2011' is not valid for DtNews" because the system swaps days and months ... I need to have dd/mm/yyyy format.

1 Answer 1

1

You could set the culture in your web.config to some culture for which the date format is dd/MM/yyyy. For example that's the case with it-IT:

<globalization culture="it-IT" uiCulture="it-IT"/>

By default the culture is set to auto. This means that ASP.NET uses the browser settings in order to deduce the culture of the client. So if the browser is configured to use en-US, then the format for dates will be MM/dd/yyyy.

Another possibility is to set the format for the current thread culture:

var dtfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
dtfi.ShortDatePattern = "dd/MM/yyyy";
dtfi.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dtfi;
Thread.CurrentThread.CurrentUICulture.DateTimeFormat = dtfi;
Sign up to request clarification or add additional context in comments.

1 Comment

globalization doesn't work for me. The second variant throws exception like "property is read-only" for last 2 statements if I put it into global.asax BeginRequest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.