js获取当前日期方法

       我们经常会用到日期,下面是js获取日期的方法,后面有其他的方法还会不定期更新。

 1 // 获取当前日期时间
 2 function getCurrentDateTime(){
 3      var date=new Date();
 4      var year=date.getFullYear();
 5      var month=date.getMonth()+1;
 6      var day=date.getDate();
 7      var hours=date.getHours();
 8      var minutes=date.getMinutes();
 9      var seconds=date.getSeconds();
10      return year+"-"+formatZero(month)+"-"+formatZero(day)+" "+formatZero(hours)+":"+formatZero(minutes)+":"+formatZero(seconds);
11  }
12 
13 //获取当前日期
14 function getCurrentDate(){
15      var date=new Date();
16      var year=date.getFullYear();
17      var month=date.getMonth()+1;
18      var day=date.getDate();
19      return year+"-"+formatZero(month)+"-"+formatZero(day);
20 }
21 
22 //格式化日期时间
23  function formatZero(n){
24      if(n>=0&&n<=9){
25          return "0"+n;
26      }else{
27          return n;
28      }
29  }
View Code
原文地址:https://www.cnblogs.com/jedjia/p/js.html