JavaScript内置对象 Date()

 

Date()对象

1.创建

 

var d = new Date() // 返回当前时间
var d = new Date(milliseconds)  // 根据毫秒数创建时间
var d = new Date(dateString)  // 根据时间字符串创建时间
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds)  // 根据传入的年月日时分秒毫秒创建时间

 

2.方法

  1. var d = new Date()
    
    // 获取
    
    // 1.getFullYear() 返回当前年份
    d.getFullYear() // 2019
    
    // 2. getMonth() 返回当前月份 (0 ~ 11)
    d.getMonth()  // 2  返回月份 + 1 = 当前月份
    
    // 3. getDate() 返回一个月中的某一天 (1 ~ 31)
    d.getDate() // 当月日期 12
    
    // 3. getDay() 返回一周中的某一天 (0 ~ 6) 0为周日 1~6 对应星期1~星期6
    d.getDay()  // 2
    
    // 4. getHours() 返回当前小时数(0~23) 
    d.getHours() // 12 返回时间 + 1 = 当前时间
    
    // 5.getMinutes()	返回 Date 对象的分钟 (0 ~ 59)
    d.getMinutes() // 15
    
    // 6. getSeconds()	返回 Date 对象的秒数 (0 ~ 59)
    d.getSeconds() // 11
    
    // 7. getMilliseconds()	返回当前时间的毫秒(0 ~ 999)
    d.getMilliseconds() // 723
    
    // 8. getTime()	返回 1970 年 1 月 1 日至今的毫秒数
    d.getTIme() // 1552364111723 当前时间时间戳
    
    // 9. parse()	返回1970年1月1日午夜到指定日期(字符串)的毫秒数
    d.parse('2019-3-12 13:18:59') // 1552367939000 这个控制台打印会报错, 实际返回传入时间字符串的时间戳 
    
    // 设置
    
    
    // 10. setFullYear()	设置 Date 对象中的年份(四位数字)
    d.setFullYear(2020) // Thu Mar 12 2020 13:25:51 GMT+0800 (中国标准时间) 年份变为2020
    
    // 11 setMonth()	设置 Date 对象中月份 (0 ~ 11)
    d.setMonth(3) // 4月 Fri Apr 12 2019 13:38:02 GMT+0800 (中国标准时间) 月份变为3
    
    // 12. setDate()	设置 Date 对象中月的某一天 (1 ~ 31)
    d.setDate(13) // 13日 Wed Mar 13 2019 13:24:41 GMT+0800 (中国标准时间) 日期数变为13
    
    // 13. setHours()	设置 Date 对象中的小时 (0 ~ 23)
    d.setHours(3) // Tue Mar 12 2019 03:40:58 GMT+0800 (中国标准时间) 小时数变为3
    
    // 14. setMinutes()	设置 Date 对象中的分钟 (0 ~ 59)
    d.setMinutes(23) // Tue Mar 12 2019 13:23:54 GMT+0800 (中国标准时间) 分钟数变为13
    
    // 15. setSeconds()	设置 Date 对象中的秒钟 (0 ~ 59)
    d.setSeconds(58) // Tue Mar 12 2019 13:43:58 GMT+0800 (中国标准时间) 秒数变为58
    
    // 16. setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)
    d.setMilliseconds(666)  // Tue Mar 12 2019 13:45:07 GMT+0800 (中国标准时间)
    
    // 17. setTime()	setTime() 方法以毫秒设置 Date 对象
    d.setTime(1552369585625) // Tue Mar 12 2019 13:46:56 GMT+0800 (中国标准时间)
    
    // 18. toJSON()	以 JSON 数据格式返回日期字符串。
    d.toJSON() // "2019-03-12T05:46:25.625Z"
    
    // 19. toLocaleDateString()	根据本地时间格式,把 Date 对象的日期部分转换为字符串
    d.toLocaleDateString() // "2019/3/12"
    
    // 20. toLocaleTimeString()	根据本地时间格式,把 Date 对象的时间部分转换为字符串
    d.toLocaleTimeString() // "下午1:46:25"
    
    // 21. toLocaleString()	据本地时间格式,把 Date 对象转换为字符串
    d.toLocaleString() // "2019/3/12 下午1:46:25"
    
    // 22. toString()	把 Date 对象转换为字符串
    d.toString() // "Tue Mar 12 2019 13:46:25 GMT+0800 (中国标准时间)"
    
    // 23. toTimeString()	把 Date 对象的时间部分转换为字符串
    d.toTimeString() //"13:46:25 GMT+0800 (中国标准时间)"
    
    // 24. valueOf() 返回 Date 对象的原始值
    d.valueOf() // 1552369585625 当前时间时间戳

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值