datepicker date off by one day

前端 未结 14 843
攒了一身酷
攒了一身酷 2020-12-01 05:22

The date returned by date picker is off by one day. Is it a problem in my code or is it a bug?

The date sent to date_picker is 2012-03-21. The date returned by dat

相关标签:
14条回答
  • 2020-12-01 05:37

    After experiencing the same issue and landing on this page, it turned out in my case it was caused by invalid labeling of the days. I started the week on Monday, instead of Sunday. I hope this helps somebody.

    0 讨论(0)
  • 2020-12-01 05:39
    var myDate = $.datepicker.parseDate("yy-mm-dd", "2013-10-21");
    ..//do whatever with myDate now
    
    0 讨论(0)
  • 2020-12-01 05:41

    It is happening due to difference in timezone with date format- yyyy-mm-dd

    new Date ('2015/07/10'); // returns: "Fri Jul 10 2015 00:00:00 GMT-0700 (Pacific Daylight Time)"
    
    new Date ('2012-07-10'); // returns: "Thu Jul 09 2015 17:00:00 GMT-0700 (Pacific Daylight Time)" 
    

    yyyy/mm/dd - is not considering timezone while calculating local time.But
    yyyy-mm-dd - is considering time while calculating local time in java script date function.
    This can be reproducible when client(browser) and server time zones are different and having timezone/date difference by 1 day.

    You can try this on your machine by changing time to different time zones where time gap b/w should be >=12 hours.

    0 讨论(0)
  • 2020-12-01 05:43

    It is not the datepicker,

    console.log(new Date('2012-03-21')); //prints Tue Mar 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
    

    The Javascript Date object can accept one of the following syntax as below,

    1. new Date()
    2. new Date(milliseconds)
    3. new Date(dateString)
    4. new Date(year, month, day [, hour, minute, second, millisecond ])

    So in your case it is going to call the dateString and parse. So try appending the time as below,

    new Date ('2012-03-21T00:00:00') //should return you Wed Mar 21 2012
    

    DEMO

    or Better to use as below,

    new Date (2012, 2, 21). 
    

    year - Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.

    month - Integer value representing the month, beginning with 0 for January to 11 for December.

    day - Integer value representing the day of the month (1-31).

    0 讨论(0)
  • 2020-12-01 05:43

    After trying many solutions the following code worked for me taken from (https://stackoverflow.com/a/14006555/1736785)

       function createDateAsUTC(date) {
        return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
    }
    
    0 讨论(0)
  • 2020-12-01 05:44

    As said Javascript January equals 0 so this would work for datepicker or input type date.

     end_date = end_date.split('-');
     end_date = new Date(end_date[0],Number(end_date[1])-1,end_date[2]);
    
    0 讨论(0)
提交回复
热议问题