Specifying the value output of of an HTML5 input type = date?

后端 未结 3 1427
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 09:47

I\'d like to add native date pickers to my application, which currently uses a legacy, home-rolled system. Date input support isn\'t widespread, yet, but if I could present

相关标签:
3条回答
  • 2020-12-03 10:21

    In Google Chrome, this has recently changed. The values displayed to the user are now based on the operating system locale. The readout, so input.value, always returns as yyyy-mm-dd regardless of the presentation format, however.

    Way more useful in my opinion.

    Source:

    http://updates.html5rocks.com/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome

    0 讨论(0)
  • 2020-12-03 10:34

    The format of the HTML date field is standard. While it is not possible to change this, you can easily convert from a date object to the format you are looking for given your user has JavaScript enabled.

    // Converts any valid Date string or Date object into the format dd-MMM-yyyy
    function dateTransform(d) {
        var s = (new Date(d)).toString().split(' ');
        return [s[2],s[1],s[3]].join('-');
    }
    

    Otherwise you will need to submit in ISO format and implement a server-side solution. Perhaps in time, this could lead to more usage of the standard date format in every locality.

    0 讨论(0)
  • 2020-12-03 10:36

    The HTML5 date input field is specific about the format it uses: RFC 3339 full-date

    Is it possible to change the legacy date picker to use yyyy-mm-dd format (and update the server side that reads that)?

    I assume not, so then you can add some Javascript glue code that listens for a change event on the HTML5 input and updates a hidden date value field to match the format of the legacy date picker (converts yyyy-mm-dd to dd-MMM-yyyy).

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