js计算指定日期几天前或者几天后的日期
分类:实例代码
本章节分享一段代码实例,它实现了计算指定日期几天前或者几天后的日期是什么的功能。
代码实例如下:
function func(date, num) { //可以加上错误处理 var odate = new Date(date) odate = odate.valueOf() odate = odate - num * 24 * 60 * 60 * 1000 odate = new Date(odate) console.log(odate.getFullYear() + "年" + (odate.getMonth() + 1) + "月" + odate.getDate() + "日") } func("12/1/2016", 2)
上面的代码实现了我们的要求,代码非常的简单,这里就不多介绍了。