Javascript Date构造函数忽略参数

时间:2014-04-19 17:50:49

标签: javascript date constructor

据说,我应该可以使用Date构造函数创建一个任意日期,如此处所示referenced here

我哪里错了?请注意,在 prettyDateToTimeStamp 的最后几行,我修改月份和日期以验证Date构造函数正在执行某些操作 - 但它没有注意到我传入的任何内容并且只返回当前日期。

以下是我的代码:a jsfiddle

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to display the full year of todays date.</p>
<p id="demo2">todays date.</p>
<p id="demo3">some other date.</p>

<button onclick="showdates()">Try it</button>

<script>

function showdates() {
   var d = Date.now();
   var dv = document.getElementById('demo');
   dv.innerHTML = d;
   var pd = prettyDate(d);
   dv = document.getElementById('demo2');
   dv.innerHTML = pd;
   var ts = prettyDateToTimeStamp(pd);
   dv = document.getElementById('demo3');
   dv.innerHTML = ts;
}

    function prettyDate(javaScriptTimeStamp) {
      var dt = new Date(javaScriptTimeStamp);
      var year    = dt.getFullYear();
      var month   = dt.getMonth() + 1;
      var day     = dt.getDate();
      var hours = dt.getHours();
      var minutes = dt.getMinutes();
      var seconds = dt.getSeconds();
      return month + "/" + day + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
   }

   function prettyDateToTimeStamp(prettyDate) {
      var halves = prettyDate.split(' ');
      console.log("halves: " + halves);
      var calpart = halves[0];
      console.log("calpart : " + calpart );
      var clockpart = halves[1];
      console.log("clockpart : " + clockpart );
      var calbits = calpart.split('/');
      console.log("calbits : " + calbits );
      var timebits = clockpart.split(':');
      console.log("timebits : " + timebits );
      var year = parseInt(calbits[2],10);
      console.log("year : " + year );
      var month = parseInt(calbits[0],10);
      console.log("month : " + month );
      var day = parseInt(calbits[1],10);
      console.log("day : " + day );
      var hour = parseInt(timebits[0],10);
      console.log("hour : " + hour );
      var min = parseInt(timebits[1],10);
      console.log("min : " + min );
      var sec = parseInt(timebits[2],10);
      console.log("sec : " + sec );
      month += 3; // change month radically to demonstrate the problem
      console.log("month is now: " + month );
      day += 7; // change day too
      console.log("day is now: " + day );
      var ts = Date(year,month,day,hour,min,sec,0);
      console.log("ts : " + ts ); // date ctor paramters completely ignored...?
      return ts;
   }

</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

omg,我不得不说“新”日期....(我最近一直在使用Python太多了)

现在可以使用更正的代码。

   function prettyDateToTimeStamp(prettyDate) {
      var halves = prettyDate.split(' ');
      var calpart = halves[0];
      var clockpart = halves[1];
      var calbits = calpart.split('/');
      var timebits = clockpart.split(':');
      var year = parseInt(calbits[2],10);
      var month = parseInt(calbits[0],10);
      var day = parseInt(calbits[1],10);
      var hour = parseInt(timebits[0],10);
      var min = parseInt(timebits[1],10);
      var sec = parseInt(timebits[2],10);
      month += 3; // change month radically to demonstrate the problem
      day += 7; // change day too
      var ts = new Date(year,month,day,hour,min,sec,0); // you have to use NEW here!
      return ts;
   }