构造函数

  1. new Date(); // 当前日期和时间
  2. new Date(milliseconds) // 根据时间戳创建
  3. new Date(dateString) // 根据date字符串创建
  4. new Date(year,monthIndex[,day[,hours,[minutes,[seconds[,milliseconds]]]]])
  5. // 指定年月日时分秒创建,除了day默认1,其他的默认为0;月份对应0-11
  6. +new Date(); // 直接转为时间戳

dateString

是一个String类型的格式化date字符串,该字符串需要能被Date.parse()识别

"2019/9/9"
"2019/09/09"
"2019-9-9"
"2019-09-09" 
"2019/09/09 12:20:05"           // 推荐
"February 3,2009 12:30:15"
  • 建议使用 yyyy/MM/dd hh:mm:ss 格式,其他的可能会出现意想不到的错误

实例方法

格式化时间

Date.prototype.toLocalString()       // "2020/6/20 下午4:41:30"
  • 根据系统时间,把Date对象转为字符串返回

    Date.prototype.toLocaleDateString()  // "2020/6/20"
    
  • 根据系统时间,把Date对象的日期部分转为字符串返回

    Date.prototype.toLocaleTimeString()  // "下午4:41:30"
    
  • 根据系统时间,把Date对象的时间部分转为字符串返回

获取部分信息

Date.prototype.getFullYear()        // 返回年,数字
Date.prototype.getMonth()           // 返回月,0-11
Date.prototype.getDate()            // 返回日,数字
Date.prototype.getDay()             // 返回星期,1-6正常,0周日
Date.prototype.getHours()           // 返回小时,24小时制,数字
Date.prototype.getMinutes()         // 返回分钟,数字
Date.prototype.getSeconds()         // 返回秒,数字
Date.prototype.getTime()            // 返回时间戳

静态方法

Date.now()
  • 获取系统当前时间的时间戳