正在设置未来的Javascript日期



我正在编辑倒计时时钟,但不确定如何用javascript设置未来的日期。为下一个新年树立榜样。我需要把它定为一个固定的日期,2015年4月27日。

            // Grab the current date
            var currentDate = new Date();
            // Set some date in the future. In this case, it's always Jan 1
            var futureDate  = new Date(currentDate.getFullYear() + 1, 0, 1);
            // Calculate the difference in seconds between the future and current date
            var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;

我只是不确定如何将未来的日期正确设置为2015年4月27日。

谢谢!

Matt

var d = new Date();
d.setDate(27); 
d.setMonth(3); 
d.setYear(2015); 
console.log(d);

这就是在日期中添加天数的方法:

var currentDate = new Date();
futureDate.setDate(currentDate.getDate() + 10 /* 10 days */ );

你只是想把它设置到明年的4月27日吗?如果是这样的话,这应该有效。

var futureDate  = new Date(currentDate.getFullYear() + 1, 3, 27);

工作JS Fiddle:

http://jsfiddle.net/Kitchenfinks/xukwLbdk/

最新更新