admin管理员组

文章数量:1405393

I am trying to get the date which is 14th of January,2014 in Javascript but it is returning invalid date.

var clock;
$(document).ready(function () {
    var currentDate = new Date();
    var futureDate = new Date(currentDate.setFullYear(2014,17,1) + 1, 0, 1);
    alert(futureDate);
});

Jsfiddle:/

I am trying to get the date which is 14th of January,2014 in Javascript but it is returning invalid date.

var clock;
$(document).ready(function () {
    var currentDate = new Date();
    var futureDate = new Date(currentDate.setFullYear(2014,17,1) + 1, 0, 1);
    alert(futureDate);
});

Jsfiddle:http://jsfiddle/zJF35/

Share Improve this question edited Jan 6, 2014 at 18:40 Barmar 784k57 gold badges548 silver badges660 bronze badges asked Jan 6, 2014 at 18:39 Prithviraj MitraPrithviraj Mitra 11.9k15 gold badges63 silver badges107 bronze badges 4
  • That's plain Javascript, not jQuery, except for the $(document.ready() part. – Barmar Commented Jan 6, 2014 at 18:40
  • Your code does not match up with the JSFiddle example. Please change either. – Loyalar Commented Jan 6, 2014 at 18:41
  • setFullYear only takes one argument, why are you giving it 3? – Barmar Commented Jan 6, 2014 at 18:41
  • 1 The fiddle is fixed up here – Abraham Hamidi Commented Jan 6, 2014 at 18:41
Add a ment  | 

4 Answers 4

Reset to default 4

Try var futureDate = new Date(new Date().getFullYear(), 0, 14);

setFullYear() method sets the full year for a specified date according to local time.

dateObj.setFullYear(yearValue[, monthValue[, dayValue]])

here the middle one parameter is month value in which you are passing 17 which is not making sense and its converting into next year.

So You probably want this

var futureDate = new Date(currentDate.setFullYear(2014,0,14));

Js Fiddle Demo

You can try

var futureDate = new Date(2014,0,14);

Any future date in JavaScript (postman test uses JavaScript) can be retrieved as:

var dateNow = new Date();  
var twoWeeksFutureDate = new Date(dateNow.setDate(dateNow.getDate() + 14)).toISOString();

//Note it returns 2 weeks future date from now.

本文标签: Get future date using JavascriptStack Overflow