zoukankan      html  css  js  c++  java
  • 时间戳与日期互转

    1、将日期格式转换成时间戳格式:

    1 var date = new Date(),
    2     // 三种方式得到时间戳
    3     time1 = date.getTime(),
    4     time2 = date.valueOf(),
    5     time3 = Date.parse(date)
    6         
    7 console.log(time1) // 1536562927967
    8 console.log(time2) // 1536562927967
    9 console.log(time3) // 1536562927000        

    3种获取时间戳的区别:

    time1、time2获取到的时间戳会精确到毫秒;

    而time3只能精确到秒,后边三位毫秒数用000替代

    注:获取到的时间戳除以1000就可获的Unix时间戳,就可传值给后台。

    2、将时间戳格式转换成日期格式:

     1 function timestampToTime (timestamp) {
     2      // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
     3      var date = new Date(timestamp * 1000),
     4          Y = date.getFullYear() + '-',
     5          M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
     6          D = date.getDate() + ' ',
     7          h = date.getHours() + ':',
     8          m = date.getMinutes() + ':',
     9          s = date.getSeconds()
    10 
    11       return Y + M + D + h + m + s
    12 }
    13 timestampToTime(1536562927)
    14 console.log(timestampToTime(1536562927)) // 2018-09-10 15:2:7

    注:如果是Unix时间戳记得乘以1000。eg:PHP函数time()获得的时间戳就要乘以1000。

  • 相关阅读:
    Yarn架构基本概况(二)
    Yarn架构基本概况(二)
    Yarn架构基本概况(二)
    Yarn架构基本概况(一)
    Yarn架构基本概况(一)
    Yarn架构基本概况(一)
    从Hadoop 安全机制原理到当今主流安全机制
    WebService数据示例
    CXF浅析
    WebService的网络协议
  • 原文地址:https://www.cnblogs.com/cq-0715/p/9619816.html
Copyright © 2011-2022 走看看