开发过程中,比较常见的功能,比如小程序选择时间,自定义时间范围等等,js获取N天之前或者N天之后的日期
实例
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> <script type="text/javascript"> function fun_date(num) { var date1 = new Date(); //今天时间 var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate() console.log(time1); var date2 = new Date(date1); date2.setDate(date1.getDate() + num); //num是正数表示之后的时间,num负数表示之前的时间,0表示今天 var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate(); console.log(time2); return time2; } alert(fun_date(7)); </script> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例