想要使用JavaScript获取指定时间向前推三个月的日期,可以先选择date对象,然后使用getMonth()方法获取月份,最后从getMonth()方法中减去三个月,并返回日期。
方法1:获取当前日期三个月前的日期
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> </head> <body style="text-align:center;"> <p id="UP" style="font-size: 19px; font-weight: bold;"> </p> <p id="DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script> var up = document.getElementById('UP'); var down = document.getElementById('DOWN'); var d = new Date(); up.innerHTML = "今天的日期为: "+ d.toLocaleDateString(); d.setMonth(d.getMonth() - 3); down.innerHTML = "3个月前,日期为:"+ d.toLocaleDateString(); </script> </body> </html>
方法2:获取指定日期三个月前的日期
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> </head> <body style="text-align:center;"> <p id="UP" style="font-size: 19px; font-weight: bold;"> </p> <p id="DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script> var up = document.getElementById('UP'); var down = document.getElementById('DOWN'); var d = new Date("2010/12/02"); up.innerHTML = "指定日期为: "+ d.toLocaleDateString(); d.setMonth(d.getMonth() - 3); down.innerHTML = "3个月前,日期为:" + d.toLocaleDateString(); </script> </body> </html>
网友评论文明上网理性发言 已有0人参与
发表评论: