js 獲取時間日期今天明天后天
阿新 • • 發佈:2019-02-01
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <script > function getDateStr(dayCount){ if(null == dayCount){ dayCount = 0; } var dd = new Date();
dd.setDate(dd.getDate()+dayCount);//設定日期 var y = dd.getFullYear(); var m = dd.getMonth()+1;//獲取當前月份的日期 var d = dd.getDate(); return y+"-"+m+"-"+d; } document.write("<br/> 昨天:" + getDateStr(-1)); document.write("<br />今天:"+getDateStr(0)); document.write("<br />明天:"+getDateStr(1)); document.write("<br />後天:"+getDateStr (2)); </script> </body></html>
dd.setDate(dd.getDate()+dayCount);//設定日期 var y = dd.getFullYear(); var m = dd.getMonth()+1;//獲取當前月份的日期