Prithviraj Mitra
Prithviraj Mitra

Reputation: 11812

Get future date using Javascript

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.net/zJF35/

Upvotes: 2

Views: 5737

Answers (4)

Dheeraj
Dheeraj

Reputation: 342

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.

Upvotes: 0

Stefano Ortisi
Stefano Ortisi

Reputation: 5326

You can try

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

Upvotes: 1

Sachin
Sachin

Reputation: 40970

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

Upvotes: 1

Abraham Hamidi
Abraham Hamidi

Reputation: 13789

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

Upvotes: 4

Related Questions