html5 - Cross-browser datepicker format in asp.net mvc -
to chrome , other browsers support html5 date natively show datepicker need put these attributes on date property in model:
[datatype(datatype.date)] [displayformat(applyformatineditmode = true, dataformatstring = "{0:yyyy-mm-dd}")] public datetime? { get; set; }
for other browsers use datepicker jquery ui.
i have set datepicker use dd.mm.yyyy format, dd.mm.yyyy.
it works long users clicks field , chooses date, when there data in field server-side shown in yyyy-mm-dd format , not chosen default value when datepicker opened.
how show dates in dd.mm.yyyy format when has sent server in yyyy-mm-dd format work native html5 datepicker?
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <link href="content/themes/base/jquery-ui.css" rel="stylesheet" /> <script src="scripts/jquery-1.10.2.js"></script> <script src="scripts/modernizr-2.6.2.js"></script> <script src="scripts/jquery-ui-1.10.2.js"></script> <script src="scripts/jquery-ui-i18n.js"></script> <script type="text/javascript"> $(document).ready(function () { if (!modernizr.inputtypes.date) { $.datepicker.setdefaults( $.extend( { 'dateformat': 'mm.dd.yy', 'altformat': 'yy-mm-dd' }, $.datepicker.regional['no'] ) ); $("#from").datepicker().val(); $("#to").datepicker(); } }) </script> </head> <body> <form action="/home/date" method="post"> <label for="from">from: </label> <input id="from" name="from" type="date" value="2014-09-23" /> <label for="to">to: </label> <input id="to" name="to" type="date" value="2014-10-09" /> <input type="submit" value="save" class="btn btn-default" /> </form> </body> </html>
in chrome uses native date-picker , formats date according chromes settings.
in firefox renders this:
after have clicked in field , chosen date uses format want, dd.mm.yyyy
is there way force jquery datepicker format included data on page load?
you can change current culture in global.asax file, application level example can try format wanted,
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; }
Comments
Post a Comment