显示DD.MM.YYYY日期+ X天

时间:2017-03-19 02:13:31

标签: javascript

您好我有一个代码,以DD.MM格式显示日期+ 5天

请帮我将当前年份添加到此代码中。 DD.MM.YYYY

年份也应该像日期和月份一样+ "2017"

<script>
function get(dday) {
 var    newdate = new Date();
     newdate.setDate(newdate.getDate()+dday);
return newdate.getDate() +  "." + ''+['01','02','03','04','05','06','07','08','09','10','11','12'][newdate.getMonth()];
}
  </script>


<span><script  type="text/javascript">document.write(get(5));</script></span>

2 个答案:

答案 0 :(得分:0)

Date.getFullYear返回全年。

<script>
function get(dday) {
 var newdate = new Date();
 newdate.setDate(newdate.getDate()+dday);
 function pad(value){
  return ("0"+value).substring(value<10?0:1);
 }
 return [pad(newdate.getDate()),pad(newdate.getMonth()+1),newdate.getFullYear()].join(".");
}
  </script>


<span><script  type="text/javascript">document.write(get(5));</script></span>

答案 1 :(得分:0)

试试这个 -

<script>
function get(dday) {
 var    newdate = new Date();
     newdate.setDate(newdate.getDate()+dday);
return newdate.getDate() +  "." + ''+['01','02','03','04','05','06','07','08','09','10','11','12'][newdate.getMonth()]+"."+newdate.getFullYear();
}
  </script>


<span><script  type="text/javascript">document.write(get(5));</script></span>

输出:24.03.2017