Skip to content Skip to sidebar Skip to footer

Formatting Datetime On The Clients Sends Default Date Value On Form Post

I'm trying to follow this example to correctly apply datetime format I have view model which contains datetime property public class MyViewModel { [DisplayFormat(ApplyFormatInE

Solution 1:

.NET is parsing dates (and numbers) according to the current culture set. The culture in ASP.NET depends not on the browsers culture but on the culture the ASP.NET thread is running. So if you want to pass dates in a specific format there are (at least) two options:

  1. Set the current thread's culture (or at least the date format) to a specific value. For example (this should match your example):

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string cultureName = CultureHelper.GetImplementedCulture("de-DE"); 
        Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }
    

    please find CultureHelper from here and do not forget to add your supported languages (see list _cultures).

  2. Create a model binder that filters DateTime fields according to your needs. An example can be found here.

EDIT: Updated example


Post a Comment for "Formatting Datetime On The Clients Sends Default Date Value On Form Post"