JavaScript日期对象详解

159 阅读3分钟

时间对象

    let date = new Data()
    console.log(typeof data) // object
    console.log(data) // 自动转为字符串在控制台输出 Tue Jan 03 2023 14:11:02 GMT+0800 (中国标准时间)
    // tue 表示星期几
    // jan 表示几月
    // 03 表示几号
    // GMT+0800 GMT表示格林时间(标准时间)+0800是因为中国在东八区所以+8

传参

    1个传参 毫秒数
    let date = new Date(1000)
    console.log(date) // Thu Jan 01 1970 08:00:01 GMT+0800 (中国标准时间)
    // 1970 1 1 8:0:0 在此时间基础上加上一秒钟
    // 为什么在此基础上计时,为了纪念unix系统等
    // 为什么是八点呢,因为咱们是东八区,所以在此基础上加
    
    两个参数 三个参数 等等
    let date = new Date(2023,10) 
    console.log(date) // Wed Nov 01 2023 00:00:00 GMT+0800 (中国标准时间)
    let date = new Date(2023,10,1) 
    console.log(date) // Fri Nov 03 2023 00:00:00 GMT+0800 (中国标准时间)
    let date = new Date(2023,10,3,10,10,10) 
    console.log(date) // Fri Nov 03 2023 10:10:10 GMT+0800 (中国标准时间)
    
    字符串
    let date = new Date('2023-10-10 10:10:10')
    let date2 = new Date('2023/10/10 10:10:10')
    console.log(date) // Tue Oct 10 2023 10:10:10 GMT+0800 (中国标准时间)
    console.log(date2) // Tue Oct 10 2023 10:10:10 GMT+0800 (中国标准时间)

常用方法

    获取方法
    let date = new Date()
    // 获取年
    console.log(date.getFullYear())
    // 获取月 0-11 => 1-12
    console.log(date.getMonth())
    // 获取几号
    console.log(date.getDate())
    // 星期几 周日0 周一 - 周六 1-6
    console.log(date.getDay())
    // 时
    console.log(date.getHours())
    // 分
    console.log(date.getMinutes())
    // 秒
    console.log(date.getSeconds())
    // 获取毫秒数
    console.log(date.getMilliseconds())
    // 获取时间戳
    console.log(date.getTime())
    
    设置方法
    let date = new Date()
    // 设置年
    date.setFullYear(2024)
    // 设置月 0-11 => 1-12
    date.setMonth(8)
    // 设置几号
    date.setDate(20)
    // 时
    date.setHours(12)
    // 分
    date.setMinutes(30)
    // 秒
    date.setSeconds(50)
    console.log(date) // Fri Sep 20 2024 12:30:50 GMT+0800 (中国标准时间)
    // 设置时间戳
    date.setTime(6455544511255)
    console.log(date) // Wed Jul 27 2174 06:48:31 GMT+0800 (中国标准时间)
    

定时器

    倒计时定时器
    // 延时定时器
    setTimeout(()=> { // 注册定时器
        console.log("setTimeout")
    },1000) // 过一秒钟执行 

    // 间隔定时器
    setInterval(()=> { // 注册定时器
        console.log("setInterval")
    },1000) // 每过一秒钟执行 
    
    // 返回值
    let time1 = setTimeout(()=> { 
        console.log("setTimeout")
    },1000) 

    let time2 = setInterval(()=> { 
        console.log("setInterval")
    },1000) 
    console.log(time1) // 返回值是1 表示第一个定时器
    console.log(time2) // 返回值是2 表示第二个定时器
    
    // 清除定时器
    clearTimeout(time1) 
    clearInterval(time2)
    

倒计时

    // 当前时间
    let currentDate = new Date()
    // 指定时间
    let targetDate = new Date('2023-2-1')

    function diffTime(currentDate, targetDate) {
        // 时间戳 不需要调用getTime 相减会自动得到时间戳
        let sub = Math.floor((targetDate - currentDate) / 1000) // /1000 获取秒;
        // 天 
        let day = parseInt(sub / (60 * 60 * 24))
        // 小时 
        let hours = parseInt((sub - (60 * 60 * 24 * day)) / (60 * 60))
        // 分
        let minutes = parseInt((sub - (60 * 60 * 24 * day) - (60 * 60 * hours)) / 60)
        // 秒
        let seconds = parseInt((sub - (60 * 60 * 24 * day) - (60 * 60 * hours) - (60 * minutes)))
        let obj = {
            day,
            hours,
            minutes,
            seconds,
        }
        return obj;
    }
    let date = diffTime(currentDate, targetDate) // {day: 28, hours: 8, minutes: 53, seconds: 10}