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

      1、时间戳转日期格式:

      

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

      2、日期转时间戳:

      

    1 var date = new Date('2014-04-23 18:55:49:123');
    2     // 有三种方式获取
    3     var time1 = date.getTime();
    4     var time2 = date.valueOf();
    5     var time3 = Date.parse(date);
    6     console.log(time1);//1398250549123
    7     console.log(time2);//1398250549123
    8     console.log(time3);//1398250549000
  • 相关阅读:
    PHP开发APP接口(九)
    C#深入理解类型
    C#从委托、lambda表达式到linq总结
    C# ==和Equals()
    C# 泛型
    C# Reflection
    原声JS网络请求
    JavaScript预编译
    泛型初探
    C#内存分配
  • 原文地址:https://www.cnblogs.com/nelsonlei/p/8656495.html
Copyright © 2011-2022 走看看