jquery UI Datepicker setDate 仅适用于 Google Chrome
jquery ui datepicker setdate only works on google chrome
我的代码:
<input type="text" id="testdate" name="testdate" value="2017-01-01" />
<script>
$('#testdate').datepicker();
$('#testdate').datepicker('option', 'dateFormat', 'yy-mm-dd');
$("#testdate").datepicker('setDate', new Date('2017-01-01 12:00:00'));
</script>
JS小提琴:https://jsfiddle.net/g0uyok13/
在谷歌浏览器中,它在文本输入区域中显示2017-01-01。在Firefox中,它显示今天的日期(atm 2016-02-24)。我希望它在Firefox中的工作方式与在Google Chrome中的工作方式相同。
有什么想法吗?
问题出在时间12:00:00
.不要忘记它是您正在使用的日期选择器。时间与它无关。
现在它在 chrome 中工作的原因,我想它是浏览器的内部实现。Mozilla丢弃了无效的日期格式,而chrome尝试将其削减...(这纯粹是我的假设,我不基于任何文档......然而)
更新
是的,我的假设是对的。Firefox不支持您输入的日期格式,而chrome更灵活并且确实支持它。对于通用日期和时间,您可以使用以下代码:
$('#testdate').datepicker();
$('#testdate').datepicker('option', 'dateFormat', 'yy-mm-dd');
$("#testdate").datepicker('setDate', new Date('2017-01-01T12:00:00Z'));
(适用于两种浏览器)
您还可以看到新的Date()在Chrome和Firefox中的工作方式不同,以获取更多信息
您可以尝试将日期字符串的格式更改为以下格式:
$("#testdate").datepicker('setDate', new Date('2017-01-01T12:00:00'));
火狐不知道如何用空格解析日期
这是来自MDN的摘录
日期字符串
表示日期的字符串值。字符串应位于 Date.parse() 方法(符合 IETF 的 RFC 2822)识别的格式 时间戳以及 ISO8601) 的版本。
您可以在 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
您的日期在火狐中无效...试试这个:
$('#testdate').datepicker();
$('#testdate').datepicker('option', 'dateFormat', 'yy-mm-dd');
$("#testdate").datepicker('setDate', new Date(2017, 00, 01, 12, 0, 0, 0));
注意:月份以 0 为基础,因此 0 是 1 月 1 日是 2 月等...
这应该在Firefox和chrome中工作。我从这个堆栈溢出帖子中获取了日期格式:链接
在火狐中试试这个:
console.log(new Date('2017-01-01 12:00:00'));
检查您的主机,它应该显示:
无效日期
工作小提琴
- JQueryhide()不适用于Mozzilla,但适用于Chrome
- PHP中的setcookie仅适用于localhost
- html5 drawImage适用于firefox,而不是chrome
- 提供“;onClick"适用于iPad(触摸屏)和桌面用户的默认功能
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- ResolveUrl是否适用于././也
- Firebase updateChildValues适用于IOS,但不适用于Web和Android
- JavaScript警报适用于int,但不适用于string
- SQLite插件适用于Mac和Windows,但不适用于手机上的Safari
- 如何调试遗留的javascript代码(适用于IE,但在Google Chrome中不正常)
- jquery UI Datepicker setDate 仅适用于 Google Chrome
- Javascript / Ajax适用于Mozilla firefox,但不适用于Google Chrome和IE
- SOAP 客户端仅适用于 Google Chrome
- 如何使用适用于jQuery和jQuery Mobile的Google Map v3插件获取路线
- Google图表中的自定义工具提示只适用于focustarget:category
- Rails:上传dropzone,S3,carrierwave,不适用于Safari,但适用于Google Chrome
- Google Places componentrestrictions适用于iOS的国家/地区
- Kinetic.js javascript不适用于Google Chrome,但适用于Mobile Safari
- @页边距设置仅适用于Google Chrome
- Google HTML 5 地理位置“使用我的位置”仅适用于 Firefox