Change date format in ASP.NET MVC application

孤者浪人 提交于 2019-11-30 05:02:17

The following should work:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime? ServiceCreatedFrom { get; set; }

and in your editor template:

@model DateTime?
@Html.TextBox(
    string.Empty, 
    ViewData.TemplateInfo.FormattedModelValue, 
    new { @class = "date" }
)

and then:

@Html.EditorFor(x => x.ServiceCreatedFrom)

The second argument you were passing to the EditorFor call doesn't do what you think it does.

For this custom editor template, since you specified the format explicitly on your view model property the <globalization> element in your web.config and the current thread culture will have 0 effect. The current thread culture is used with the standard templates and when you didn't override the format with the [DisplayFormat] attribute.

As a potential aid for identifying the issue, are you able to: 1. Set a breakpoint at the point where you're trying to format the date 2. Use something like the Immediate Window in Visual Studio to evaluate the value of

Thread.CurrentThread.CurrentCulture.Name

If you do this, does it come back with the "ru-RU" culture?

I'm sure I'm not the only one that would be happy to help you work through debugging this. That said, perhaps somebody quicker than me can see the problem straight away :).

Edit: It looks like you're using Razor, so you should be able to set a breakpoint directly in the view file on the line where you're trying to format the date.

Edit #2:

There may be a cleaner way to do this, but if the form data is being posted in dd.MM.yyyy then you might need a custom model binder, something like:

public class CustomModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
              // custom bind the posted date    
        }
}

...which would then get assigned as a model binder in e.g. ApplicationStart in Global.asax.cs.

Let me know if you think this might help and I can elaborate.

stom

finally this one worked for me to get this dd/mm/yyyy format i used date = string.Format("{0}/{1}/{2}", Model.Value.Day, Model.Value.Month, Model.Value.Year);

first create EditorTemplates folder in Sharedfolder, then create a Datetime editor template in Sharedfolder/EditorTemplates/Datetime.cshtml then follow the above link.

in view

@Html.EditorFor(x => x.ServiceCreatedFrom)

hope helps someone.

You can change the current culture in your Global.asax file, for application level For Example,

using System.Globalization;
using System.Threading;

protected void Application_BeginRequest(Object sender, EventArgs e)
{    
  CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
  newCulture.DateTimeFormat.DateSeparator = "-";
  Thread.CurrentThread.CurrentCulture = newCulture;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!