js实现日期加1
直接附代码
方法1:
<html> <head> <script> function getLocalTime(addNum){//参数为加几天 var today,ms,thatDay, y, m, d,endDate; today = new Date().getTime(); ms = today + addNum*24*60*60*1000; thatDay = new Date(ms); y = thatDay.getFullYear(); m = thatDay.getMonth()+1; d = thatDay.getDate(); endDate = y+"-"+m+"-"+d; alert(endDate); //return endDate; } </script> </head> <body> <input type='text' id='a' onclick="getLocalTime('1')"/> </body> <html>
方法2:
<html> <head> <script> function getLocalTime(addNum){//参数为加几天 var d2=document.getElementById("aa").value; var arys= new Array(); arys=d2.split('-'); //得到日期类型 var myDate = new Date(arys[0],arys[1],arys[2]); myDate=myDate.valueOf(); myDate=myDate +addNum * 24 * 60 * 60 * 1000; myDate = new Date(myDate); alert(myDate.getFullYear() + "-" + (myDate.getMonth()) + "-" + myDate.getDate() ); } </script> </head> <body> <input type='text' id='aa' value='2017-7-5' onclick="getLocalTime('1')"/> </body> <html>
本文原创,转载必追究版权。