Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:35:38+00:00 2026-06-07T04:35:38+00:00

How can I force the format of datetime in asp.net mvc 4 ? In

  • 0

How can I force the format of datetime in asp.net mvc 4 ?
In display mode it shows as I want but in edit model it doesn’t.
I am using displayfor and editorfor and applyformatineditmode=true with dataformatstring=”{0:dd/MM/yyyy}”
What I have tried:

  • globalization in web.config (both of them) with my culture and uiculture.
  • modifying the culture and uiculture in application_start()
  • custom modelbinder for datetime

I have no idea how to force it and I need to input the date as dd/MM/yyyy not the default.

MORE INFO:
my viewmodel is like this

    [DisplayName("date of birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? Birth { get; set; }

in view I use @Html.DisplayFor(m=>m.Birth) but this works as expected (I see the formatting)
and to input the date I use @Html.EditorFor(m=>m.Birth) but if I try and input something like 13/12/2000 is fails with the error that it is not a valid date (12/13/2000 and 2000/12/13 are working as expected but I need dd/MM/yyyy).

The custom modelbinder is called in application_start() b/c I don’t know where else.

Using <globalization/> I have tried with culture="ro-RO", uiCulture="ro" and other cultures that would give me dd/MM/yyyy.
I have also tried to set it on a per thread basis in application_start() (there are a lot of examples here, on how to do this)


For all that will read this question:
It seems that Darin Dimitrov’s answer will work as long as I don’t have client validation.
Another approach is to use custom validation including client side validation.
I’m glad I found this out before recreating the entire application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:35:41+00:00Added an answer on June 7, 2026 at 4:35 am

    Ahhhh, now it is clear. You seem to have problems binding back the value. Not with displaying it on the view. Indeed, that’s the fault of the default model binder. You could write and use a custom one that will take into consideration the [DisplayFormat] attribute on your model. I have illustrated such a custom model binder here: https://stackoverflow.com/a/7836093/29407


    Apparently some problems still persist. Here’s my full setup working perfectly fine on both ASP.NET MVC 3 & 4 RC.

    Model:

    public class MyViewModel
    {
        [DisplayName("date of birth")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? Birth { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel
            {
                Birth = DateTime.Now
            });
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    View:

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.Birth)
        @Html.EditorFor(x => x.Birth)
        @Html.ValidationMessageFor(x => x.Birth)
        <button type="submit">OK</button>
    }
    

    Registration of the custom model binder in Application_Start:

    ModelBinders.Binders.Add(typeof(DateTime?), new MyDateTimeModelBinder());
    

    And the custom model binder itself:

    public class MyDateTimeModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    
            if (!string.IsNullOrEmpty(displayFormat) && value != null)
            {
                DateTime date;
                displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
                // use the format specified in the DisplayFormat attribute to parse the date
                if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    return date;
                }
                else
                {
                    bindingContext.ModelState.AddModelError(
                        bindingContext.ModelName,
                        string.Format("{0} is an invalid date format", value.AttemptedValue)
                    );
                }
            }
    
            return base.BindModel(controllerContext, bindingContext);
        }
    }
    

    Now, no matter what culture you have setup in your web.config (<globalization> element) or the current thread culture, the custom model binder will use the DisplayFormat attribute’s date format when parsing nullable dates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I want to make a small test window app that can force IE
Okay, I've got my normal app which is in portrait mode. I can force
For some reason, ASP.NET code on my server is now returning a format of
When using SharePoint DLLs (which are x86 only) from a non-ASP.NET app, you can
I am using C# ASP.NET 4. So far as I can tell, the HttpWebRequest
On Visual studio I can force use of 32-bit time_t by declaring _USE_32BIT_TIME_T .
Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion
I've been trying to figure WIF out for a while now, and can force
can I force the DBML designer to use my custom types instead of the
Can I force the gcc to use a null canary or terminator canary, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.