【LX实习】JS中日期对象相关知识

177 阅读1分钟

new Date()

    // 获取本地 中国标准时间 Sun Jan 05 2020 22:10:16 GMT+0800 (中国标准时间)
    let time = new Date()
    
    // 将不同格式的转化为 中国标准时间
    let a = new Date(2020, 1, 12, 12, 12, 12)
    
    let b = new Date('2020-1-12 12:12:12')
    
    // 苹果不能识别 - 只能识别 / 要做兼容性处理
    let c = new Date('2020/01/12 12:12:12')
    
    console.log(a)
    console.log(b)
    console.log(c)
    
    // 获取时间戳
    let timeStamp = time.getTime()

    // 将时间戳转化为 中国标准时间
    let start = new Date(1578233292770)

    // 标准时间可以比较,可以加减
    console.log(time - start, timeStamp)

    // 周几 (0:周天 6:周六)
    let week = time.getDay()
    
    // 年
    let year = time.getFullYear()
    
    // 月 (0:一月 11:十二月)
    let month = time.getMonth()
    
    // 日
    let day = time.getDate()
    
    // 时
    let hour = time.getHours()
    
    // 分
    let min = time.getMinutes()
    
    // 秒
    let sec = time.getSeconds()
    
    console.log(`${year}${month + 1}${day}${hour}:${min}:${sec}${week}`)
    
    // 注意:要进行判断小于'10'时要在前面加'0'