简体   繁体   English

jQuery Datepicker setDate显示不正确的日期

[英]jQuery Datepicker setDate showing incorrect date

The calendar should be showing Oct 19, 2016, but it keeps showing Oct 18, 2016. I can't figure out why! 日历应显示为2016年10月19日,但仍显示为2016年10月18日。我不知道为什么!

$('#datepickerDateReceived').datepicker({
        format: 'yyyy-mm-dd'
}).datepicker(
        'setDate', new Date('2016-10-19')
    );

http://jsfiddle.net/rt3nC/34/ http://jsfiddle.net/rt3nC/34/

The problem is that date strings are parsed as UTC 0, not local time-zone. 问题在于日期字符串被解析为UTC 0,而不是本地时区。 source 资源

To fix it, just add a time to your date string 要解决此问题,只需在日期字符串中添加一个时间

$('#datepickerDateReceived').datepicker({
        format: 'yyyy-mm-dd'
}).datepicker(
        // Initialize the date to be 00:00 local timezone on October 19, 2016
        'setDate', new Date('2016-10-19 00:00')
    );

This does not work in Safari: $('#datepickerDateReceived').datepicker('setDate', new Date('2016-10-19 00:00')); 这在Safari中不起作用:$('#datepickerDateReceived')。datepicker('setDate',new Date('2016-10-19 00:00'));

I had to put it this way to make it work in Safari: $('#datepickerDateReceived').datepicker('setDate', new Date('2016-10-19 00:00'.replace(/\\s/, 'T'))); 我必须以这种方式使其在Safari中工作:$('#datepickerDateReceived')。datepicker('setDate',new Date('2016-10-19 00:00'.replace(/ \\ s /,' T')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM