zoukankan      html  css  js  c++  java
  • Date、时间戳、格式化时间互转

    1.Date 转 时间戳:

    var d = new Date();
    var n = d.getTime();

    例如:

    let startTime = new Date(this.formData.recommendBeginTime).getTime() / 1000

    2.时间戳 转 Date
    //实例化一个Date对象,将时间戳直接传入,注意一定是13位

    var newDate = new Date (时间戳)

    例如

    time = convertTime.dateToString(new Date(time * 1000), 'dateOnly')

    3.Date 转 格式化时间

    let date = new Date
    date .getFullYear() (年)
    date .getMonth() + 1 (返回 0~11,一月到十二月)
    date.getDay() (返回0~6,周日到周六)
    date.getDate() (日)date.getHours()(时) date.getMinutes() (分) date.getSeconds() (秒)

     4.时间戳 转 字符串时间 转 时间戳

    this.queryForm.endTime = val[1].format('X') // val[1]是moment对象的时间
    let bt = `${val[0].format('YYYY')}-${val[0].format('MM')}-${val[0].format('DD')} 00:00:00`
    console.log('开始时间:', bt, Date.parse(bt))
    this.queryForm.beginTime = (bt, Date.parse(bt) / 1000) + ''

     关于时间的一些函数总结:

            /* 使用控制台测试 计算脚本执行时间 */
            console.time('for')
            for (let i = 0 ; i < 2000000; i++) {}
            console.timeEnd('for') // for: 3.680908203125ms
    
            /* 根据指定的日期与时间定义日期对象 */
            let dateObj = new Date()
            console.log('dateObj', dateObj, 'type:', typeof dateObj, ',时间戳:', dateObj*1)
            // dateObj Tue Mar 31 2020 10:36:14 GMT+0800 (中国标准时间) type: object ,时间戳: 1585622174921
            console.log('date', Date(), 'type:', typeof Date(), ',时间戳:', Date.now())
            // date Tue Mar 31 2020 10:36:14 GMT+0800 (中国标准时间) type: string ,时间戳: 1585622174921
            const date1 = new Date('1997-03-30 12:08')
            console.log(date1) // Sun Mar 30 1997 12:08:00 GMT+0800 (中国标准时间)
            console.log(date1.getMonth()) // 2
            const date2 = new Date(1997, 3, 30, 12, 08)
            console.log(date2) // Wed Apr 30 1997 12:08:00 GMT+0800 (中国标准时间)
            const param = [1997, 3, 30, 12, 08]
            const date3 = new Date(...param)
            console.log(date3) // Wed Apr 30 1997 12:08:00 GMT+0800 (中国标准时间)
    
            /* 获取时间戳的方法 */
            const date4 = new Date('1997-03-30 12:08')
            console.log(date4 * 1) // 859694880000
            console.log(Number(date4))
            console.log(date4.valueOf())
            console.log(date4.getTime())
    /* 时间戳=>时间 */
              const timestamp = date4.valueOf()
              console.log(new Date(timestamp)) // Sun Mar 30 1997 12:08:00 GMT+0800 (中国标准时间)

     时间格式化 函数

            /* date format */
            function dateFormat (date, format = 'YYYY-MM-DD HH:mm:ss') {
                const config = {
                    YYYY: date.getFullYear(),
                    MM: date.getMonth(),
                    DD: date.getDate(),
                    HH: date.getHours(),
                    mm: date.getMinutes(),
                    ss: date.getSeconds(),
                }
                for (key in config) {
                    format = format.replace(key, config[key])
                }
                return format
            }
            console.log('当前日期格式化:', dateFormat(new Date()))
            // 当前日期格式化: 2020-2-31 17:48:5
            console.log('当前日期格式化:', dateFormat(new Date(), 'YYYY年MM月DD日 HH时mm分ss秒'))
            // 当前日期格式化: 2020年2月31日 17时48分5秒
  • 相关阅读:
    HDU-2222 Keywords Search(AC自动机)
    HDU-2647 Reward(拓扑排序)
    HDU-2896 病毒侵袭(AC自动机)
    UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)
    CSU-1120 病毒(最长递增公共子序列)
    记忆化搜索
    区间动态规划 矩阵连乘 Medium
    34枚金币时间管理法
    摄影基础1
    学习法则 讲
  • 原文地址:https://www.cnblogs.com/benbendu/p/9762061.html
Copyright © 2011-2022 走看看