How to determine one year from now in Javascript

后端 未结 8 1226
醉话见心
醉话见心 2020-12-12 21:29

I\'m trying to get one year from now\'s date, and it\'s not working.

JS:

var now = new Date();

var oneYr = new Date();
oneYr.setYear(now.getYear() +         


        
8条回答
  •  情书的邮戳
    2020-12-12 21:59

    This will create a Date exactly one year in the future with just one line. First we get the fullYear from a new Date, increment it, set that as the year of a new Date. You might think we'd be done there, but if we stopped it would return a timestamp, not a Date object so we wrap the whole thing in a Date constructor.

    new Date(new Date().setFullYear(new Date().getFullYear() + 1))
    

提交回复
热议问题