时间内置函数

92 阅读1分钟
  1. Date() 返回当日的日期和时间。
 var time=new Date()
 console.log(time)

image.png

  1. getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
  2. getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。0表示星期天
  3. getMonth() 从 Date 对象返回月份 (0 ~ 11)。为了让人们看懂写程序时需要加1
  4. getFullYear() 从 Date 对象以四位数字返回年份。
  5. getYear() 3位数 后两位代表年份, 一般用getFullYear() 方法代替。
  6. getHours() 返回 Date 对象的小时 (0 ~ 23)。
  7. getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
  8. getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
 var time=new Date()
 console.log(time.getDate())//几号
 console.log(time.getDay())//周几,0表示星期天
 console.log(time.getMonth())//这里的5代表六月,因为从0开始
 console.log(time.getFullYear())//年份
 console.log(time.getYear())
 console.log(time.getHours())//时
 console.log(time.getMinutes())//分
 console.log(time.getSeconds())//秒

image.png image.png

  1. getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。1s=1000ms
 var time=new Date()
 console.log(time.getMilliseconds())
  1. getTime() 返回 1970 年 1 月 1 日至今的毫秒数。(数字绝对不会重复)
 var time=new Date()
 console.log(time.getTime())

image.png

  1. Date()可以自动识别与时间相关的数据
  var dt2 = new Date("1999/02/03 12:20:20")
  var dt3 = new Date("1999-02-03 12:20:20")
  var dt4 = new Date("1999-02-03")
  console.log(dt2)
  console.log(dt3)
  console.log(dt4)

image.png