<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>根据一个时间计算N年后的时间</title> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> </head> <body> <div> <input type="" name="" value="10" id="newYear" />10年后 </div> <div> 当前年月日时分秒:<input type="text" id="nowtime" value="2020-11-16 16:58:05"> </div> <div> 下一年年月日时分秒:<input type="text" id="nexttime" value=""> </div> </body> <script type="text/javascript"> $(function(){ $("#newYear").blur(function(){ var nowtime = $("#nowtime").val(); var newYear = $("#newYear").val(); // 分割获取年月日 var timeArr = nowtime.split(" "); // 字符串根据空格分割为数组 console.log(timeArr); var d2 = new Date(timeArr[0]); d2.setFullYear(d2.getFullYear()+parseInt(newYear)); d2.setDate(d2.getDate()-1); var sss = d2.toLocaleString(); var sssArr = sss.split(" "); sss = sssArr[0].replace(/\//g, "-") +" "+timeArr[1]; // 字符串替换字符在拼接时分秒 $("#nexttime").val(sss); }); }) </script> </html>