JS中日期对象Date(易懂)

让我为大家介绍一下日期对象吧!
日期对象,用来表示时间的对象

1.获取当前时间

    // 获取当前时间
    let date = new Date()

2.指定时间

    // 指定时间
    let date = new Date("2023-6-1 08:30:30")

日期对象的方法
因为我们日期对象返回的数据我们不能直接使用,所以需要转换为实际开发中常用的格式

方法作用说明
getFullYear()获取年份获取四位年份
getMonth()获取月份取值 0 ~ 11
getDate()获取月份中的每一天不同月份取值也不相同
getDay()获取星期取值 0 ~ 6
getHours()获取小时取值 0 ~ 23
getMinutes()获取分钟取值 0 ~ 59
getSeconds()获取秒取值 0 ~ 59
    // 获取当前时间
    let date = new Date()
    // 使用方法
    // 获取年份
    console.log(date.getFullYear())
    // 获取月份
    console.log(date.getMonth() + 1)
    // 获取日期
    console.log(date.getDate())
    // 获取星期
    console.log(date.getDay())
    // 获取小时
    console.log(date.getHours())
    // 获取分钟
    console.log(date.getMinutes())
    // 获取秒
    console.log(date.getSeconds())

时间戳
获取时间戳的三种方法
1.getTime()
2. +new Date()
3. Date.now()

	let date = new Date()
    // 1.getTime()
    console.log(date.getTime())
    // 2. +new Date()
    console.log(+new Date)
    // 3. Date.now
    console.log(Date.now())

感谢大家的阅读,本人文笔有限,如有不对的地方,可以向我指出,感谢大家!

posted @ 2023-10-17 13:21  一叶知秋04  阅读(11)  评论(0编辑  收藏  举报  来源