HTML-5 date field shows as “mm/dd/yyyy” in Chrome, even when valid date is set

前端 未结 5 1208
慢半拍i
慢半拍i 2020-12-29 00:55

I just upgraded an ASP.Net MVC application to MVC-4. The field editor for inputs based on DateTime values now include\'s the HTML-5 type="date" attrib

相关标签:
5条回答
  • 2020-12-29 01:40

    If you are dealing with a table and one of the dates happens to be null, you can code it like this:

      @{
         if (Model.SomeCollection[i].date_due == null)
           {
             <td><input type='date' id="@("dd" + i)" name="dd" /></td>
           }
           else
           {
             <td><input type='date' value="@Model.SomeCollection[i].date_due.Value.ToString("yyyy-MM-dd")" id="@("dd" + i)" name="dd" /></td>
           }
       }
    
    0 讨论(0)
  • 2020-12-29 01:43

    Had the same problem. A colleague solved this with jQuery.Globalize.

    <script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.globalize/globalize.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.globalize/cultures/globalize.culture.nl.js"></script>
    <script type="text/javascript">
        var lang = 'nl';
    
        $(function () {
            Globalize.culture(lang);
        });
    
        // fixing a weird validation issue with dates (nl date notation) and Google Chrome
        $.validator.methods.date = function(value, element) {
            var d = Globalize.parseDate(value);
            return this.optional(element) || !/Invalid|NaN/.test(d);
        };
    </script>
    

    I am using jQuery Datepicker for selecting the date.

    0 讨论(0)
  • 2020-12-29 01:45

    I was having the same problem, with a value like 2016-08-8, then I solved adding a zero to have two digits days, and it works. Tested in chrome, firefox, and Edge

    today:function(){
       var today = new Date();
       var d = (today.getDate() < 10 ? '0' : '' )+ today.getDate();
       var m = ((today.getMonth() + 1) < 10 ? '0' :'') + (today.getMonth() + 1);
       var y = today.getFullYear();
       var x = String(y+"-"+m+"-"+d); 
       return x;
    }
    
    0 讨论(0)
  • 2020-12-29 01:47

    In chrome to set the value you need to do YYYY-MM-DD i guess because this worked : http://jsfiddle.net/HudMe/6/

    So to make it work you need to set the date as 2012-10-01

    0 讨论(0)
  • 2020-12-29 02:01

    I have same problem and i found solution which is given below with full datepicker using simple HTML,Javascript and CSS. In this code i prepare formate like dd/mm/yyyy but you can work any.

    HTML Code:

        <body>
    <input type="date" id="dt" onchange="mydate1();" hidden/>
    <input type="text" id="ndt"  onclick="mydate();" hidden />
    <input type="button" Value="Date" onclick="mydate();" />
    </body>
    

    CSS Code:

    #dt{text-indent: -500px;height:25px; width:200px;}
    

    Javascript Code :

    function mydate()
    {
      //alert("");
    document.getElementById("dt").hidden=false;
    document.getElementById("ndt").hidden=true;
    }
    function mydate1()
    {
     d=new Date(document.getElementById("dt").value);
    dt=d.getDate();
    mn=d.getMonth();
    mn++;
    yy=d.getFullYear();
    document.getElementById("ndt").value=dt+"/"+mn+"/"+yy
    document.getElementById("ndt").hidden=false;
    document.getElementById("dt").hidden=true;
    }
    

    Output:

    0 讨论(0)
提交回复
热议问题