[angular-ui datepicker 指令中的 date() returns 昨天的日期

date() in [angular-ui datepicker directive] returns yesterday's date

我有使用 angular-ui datepicker directive 的日期选择器,但是当我 select 一个日期时,它会显示前一天。并通过控制台试了一下,结果依旧。

我不明白这个问题背后的主要原因。

Date 构造函数在向其传递字符串时只能识别几种格式。您需要使用 Date.parse 来识别更多的格式并生成传入日期字符串的表示形式作为自纪元以来的毫秒数,这反过来又将被构造函数接受并生成所需的日期对象.

var date = new Date(Date.parse("2015-07-27T22:00:00.000Z"));

documentation指定您需要将日期传递为

new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

因此您应该将其传递为:

new Date(2015,07,27,22,00,00,0000000000);

否则您需要使用 Date.parse 来正确解析日期。

旁注: moment.js 可以很好地处理所有这些问题。

可能对你有用

      <!doctype html>
     <html lang="en">
          <head>
        <meta charset="utf-8">
      <title>jQuery UI Datepicker - Display month &amp; year menus</title>
      <link rel="stylesheet" 
    href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
       <link rel="stylesheet" href="/resources/demos/style.css">
     <script>
      $(function() {
       $( "#datepicker" ).datepicker({
         changeMonth: true,
  changeYear: true
      });
         });
    </script>
    </head>
     <body>

   <p>Date: <input type="text" id="datepicker"></p>


  </body>
     </html>