-1

Just start doing MVC. Following the example in this page: Adding Validation in this Getting Started with ASP.NET MVC 5 tutorial.

In the model, there is a date and format as:

using System;
using System.ComponentModel.DataAnnotations;

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }

        [Display(Name = "Release Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }
}

The date format is according to this SO Answer. So (using Chorme, Edge and IE) when I in the i) Index page, ii) Details page, or even in the iii) Delete page, I see the date displayed as yyyy-MM-dd (e.g. 1959-04-15 for April 15 in 1959).

But when I go to Edit page, the date is different for different browser.

Chrome: 04/15/1959
Edge: 4/15/1959
IE: 1959-04-15

(When I view source in all 3 browsers, all show 1959-04-15.)

I think the following suggestion (according to the tutorial's description is causing the trouble, but even I remove the following culture specification, the result for all browsers are same.

<system.web>
    <globalization culture ="en-US" />
    <!--elements removed for clarity-->
</system.web>
  1. The regional setting in my computer is 'English (Hong Kong SAR)'. Short date is format as d/M/yyyy.
  2. Also when i view data in the Local DB, the date shown is '15/4/1959 0:00:00'. Looks like it uses the computer's regional setting.

Can anyone explain why the date is different in Edit mode? Also how to fix for Chrome? I know someone has suggested other methods (like adding a model, file, etc), but I just want to see if there is an easy solution without adding additional file/model. Thanks.

  • 1
    IE does not support ` –  Jun 15 '18 at 06:57
  • Note also `` (server side culture) has nothing to do with the browsers HTML-5 datepicker (which renders the date in the clients culture) –  Jun 15 '18 at 07:00
  • @StephenMuecke yes, that is a typo. Missing a 'y'. Now add back. Thanks. – Just a HK developer Jun 15 '18 at 07:03
  • After some research, here are what I find useful: i) For datetime property in model, do not go to the trouble and mark it as `DataType.Date`. ii) As @StephenMuecke has pointed out, it is better to use jQuery UI datepicker. (re: https://www.c-sharpcorner.com/UploadFile/ee01e6/how-to-add-jquery-datepicker-in-mvc/) iii) Better format all date as yyyy-mm-dd. Same for formatting in datepicker by jQuery UI. – Just a HK developer Jun 22 '18 at 03:28
  • This page https://html5tutorial.info/html5-date.php gives some background information about HTML date picker. – Just a HK developer Jun 22 '18 at 03:30

1 Answers1

1

[DataType(DataType.Date)] generates the type="date" attribute when using @Html.EditorFor() which in turn generates the browsers HTML-5 datepicker. type="date" is not supported in IE, which is why you get a textbox only with the date displayed in the specified format.

For a comparison of HTML-5 support across various browsers, refer HTML 5 TEST. Note also that FireFox has only just started supporting type="date".

As for the date being displayed in yyyy-MM-dd format in your Index and Details views, that would be because you are using @Html.DisplayFor() which uses the DataFormatString property of [DisplayFormat] to format the value.

On option in your Index and Details views is to just use

@Model.YourDate.ToString("d") // or what ever format string you want

Another alternative is to change the attribute to

DisplayFormat(DataFormatString = "{0:d}")]

so that @Html.DisplayFor() generates the correct format, and then in the Edit view, use

@Html.TextBoxFor(m => m.YourDate, "{0:yyyy-MM-dd}", new { type = "date" })

to generate the browsers HTML-5 datepicker.

But if you want a consistent datepicker across all browsers, then you need to use a jQuery plugin (for example jQuery UI).

Note also that the culture on the server has nothing to do with the browsers HTML-5 datepicker, which renders the date in the browsers culture.

  • 1) If I use `{0:d}` in DataFormatString, all pages but Edit are ok. (That is, I see d/M/yyyy - good for my regional setting). However, in the Edit mode, in Chrome, I get a string as 'mm/dd/yyyy'. Click on it will have a datepicker for the current date! But I need to have the date for the record (i.e. 15/4/1959) right in the edit box. 2) In the Edit view, should the `EditorFor()` be used? – Just a HK developer Jun 15 '18 at 07:55
  • No, you need to use `@Html.TextBoxFor(m => m.YourDate, "{0:yyyy-MM-dd}", new { type = "date" })` as per my answer –  Jun 15 '18 at 07:57
  • Sorry. Still shows up as '04/15/1959'. It is not even close to my regional setting. Looks like US regional setting? (I don't have `globalization` statement in my web.config.) – Just a HK developer Jun 15 '18 at 08:06
  • I assume you have not understood my answer. The whole purpose of the HTML-5 datepicker is to display the date in the **browsers** culture. I I open you site I would see dates in `dd/MM/yyyy` format. If a user in the US opened it, they would see it in `MM/dd/yyyy` format (which is why you need to pass the date in `yyyy-MM-dd` format). What is displayed has nothing to do with what is on your server (its client side code) –  Jun 15 '18 at 08:10
  • So again. If you want control of the format, and if you want it supported in all browsers, then use a jQuery plugin as I noted –  Jun 15 '18 at 08:11
  • I am doing all these in my machine! So the server and the client are in the same machine! Short date format for my machine is : d/M/yyyy. Say if I do not want any datepicker now (or will use the datepicker in jQuery), I just want the date displayed in Edit mode is same as others in Index/Details/Delete page. Now they are not. – Just a HK developer Jun 15 '18 at 08:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173200/discussion-between-stephen-muecke-and-user3454439). –  Jun 15 '18 at 08:16