16

I will try everything but not working this(dd/MM/yyyy) date format, this always gate mm/dd/yyyy

[Display(Name = "Release Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<DateTime> release_date { get; set; }

Razor view

@Html.EditorFor(model => model.release_date)

Using DateTime Template.

@model Nullable<System.DateTime>
@if (Model.HasValue)
{
    @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", Model.Value))
}
else
{
    @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", DateTime.Now))
}
@{
    string name = ViewData.TemplateInfo.HtmlFieldPrefix;
    string id = name.Replace(".", "_");
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#@id").datepicker({
                dateFormat: "dd/mm/yy",
                showStatus: true,
                showWeeks: true,
                highlightWeek: true,
                numberOfMonths: 1,
                showAnim: "scale",
                showOptions: {
                    origin: ["top", "left"]
                }
          });
    });
</script>
Sender
  • 6,660
  • 12
  • 47
  • 66

3 Answers3

23

You missed 2 things here:

DataFormatString="{0:dd/MM/yyyy}"; //It is dd/MM/yyyy instead of dd/mm/yyyy

and you also need:

ApplyFormatInEditMode=true

Try to use this:

[Display(Name = "Release Date")]
[DataType(DataType.Date), DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )]
public Nullable<DateTime> release_date { get; set; }
TrizZz
  • 1,200
  • 5
  • 15
  • What do you mean DataType(DataType.Date) is working? When I try this, I get a calendar to pick a date, but when I jump to the next field on the form, the date is still shown as mm/dd/yyyy. – comecme May 20 '13 at 11:14
  • I need to use dot "." as seperator. [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)] Not working? What can i do? – Can Ürek Sep 01 '15 at 14:00
  • To get this to work, I had to escape the date format as @"{0:dd\/MM\/yyyy}" – ChrisFox Sep 23 '15 at 07:45
5

{0:dd/mm/yyyy} should be {0:dd/MM/yyyy} because mm means minutes, not months:

[Display(Name = "Release Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<DateTime> release_date { get; set; }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

If you use data-annotation. when display it good. but when edit mode. it still use server format (culture setting) DisplayFormat: like it name, only for display

There are some option to solve. - paste to server as string with your favorite format - write customer bindding - Setting globalization in web.config for Format : dd/MM/yyy i using setting bellow

<system.web>
    <globalization culture="en-GB"/>
....
Wolf
  • 6,361
  • 2
  • 28
  • 25