2016-04-15 55 views
0

如何从角度js中减去当前日期的某些年份, 我想验证DOB字段,我在gmail验证中看到它是接受3年后的当前日期以便如何从角度js中减去当前日期的某些年份如何从角度js中减去当前日期的某些年份

+0

相关:http://stackoverflow.com/questions/674721/how-do-我减去分钟从JavaScript中的日期 – Fidel90

+0

这就是我问我可以减去两个日期变量,但如何计算它从目前的日期回来几年是任务离子 – Sultan

+0

这就是其他线程已经回答了,据我了解你的问题。以我的答案为例。 – Fidel90

回答

0

那么,下面是一个纯粹的JS溶液:

var nowInMS = new Date().getTime(),//get the current time and convert to milliseconds 
    threeYearsInMS = 1000 * 60 * 60 * 24 * 365 * 3,//calculate the amount of milliseconds in three years 
    threeYearsBeforeNow = new Date(nowInMS - threeYearsInMS);//build a new date after subtracting 

alert(threeYearsBeforeNow);//voila 

JSFiddle


Documentation of the Date object at MDN

相关问题