zoukankan      html  css  js  c++  java
  • JavaScrip(JS)t时间戳与时间日期间相互转换

    在javascript(js)中如何对时间戳与时间日期之间的相互转换以及用法。(转)

    javascript的getTime()方法可以输入时间戳

    利用js输入现在时间以及指定时间的时间戳

    代码

    1 //将当前的时间转换成时间戳
    2 var now = new Date();
    3 console.log(now.getTime());
    4 //将指定的日期转换成时间戳
    5 var t = "2018-07-23 20:5:30";  
    6 var T = new Date(t); 
    7 console.log(T.getTime());
    8  //console.log(),为控制台打印

    代码示图

    JavaScrip(JS)t时间戳与时间日期间相互转换

    代码运行结果

    查看浏览器控制台

    JavaScrip(JS)t时间戳与时间日期间相互转换

    利用js的时间戳,输出时间日期格式

    代码

    1 //输出现在时间的日期格式
    2 console.log(new Date());
    3 //输入一个时间戳来转换成时间日期格式
    4 //飞鸟慕鱼博客
    5  var t = 1528459530000;  
    6  console.log(new Date(t));

    代码显图

    JavaScrip(JS)t时间戳与时间日期间相互转换

     代码运行结果

    JavaScrip(JS)t时间戳与时间日期间相互转换

    关于javascript时间戳与时间日期格式转换的代码补充

    代码

     1 <script type="text/javascript">
     2     // 获取当前时间戳(以s为单位)
     3      var timestamp = Date.parse(new Date());
     4     timestamp = timestamp / 1000;
     5     //当前时间戳为:1403149534
     6      console.log("当前时间戳为:" + timestamp);
     7     // 获取某个时间格式的时间戳
     8     var stringTime = "2018-07-10 10:21:12";
     9     var timestamp2 = Date.parse(new Date(stringTime));
    10     timestamp2 = timestamp2 / 1000;
    11     //2018-07-10 10:21:12的时间戳为:1531189272
    12     console.log(stringTime + "的时间戳为:" + timestamp2);
    13     // 将当前时间换成时间格式字符串
    14      var timestamp3 = 1531189272;
    15      var newDate = new Date();
    16      newDate.setTime(timestamp3 * 1000);
    17     // Wed Jun 18 2018
    18     console.log(newDate.toDateString());
    19     // Wed, 18 Jun 2018 02:33:24 GMT
    20      console.log(newDate.toGMTString());
    21     // 2018-06-18T02:33:24.000Z
    22     console.log(newDate.toISOString());
    23     // 2018-06-18T02:33:24.000Z
    24     console.log(newDate.toJSON());
    25     // 2018年6月18日
    26     console.log(newDate.toLocaleDateString());
    27     // 2018年6月18日 上午10:33:24
    28     console.log(newDate.toLocaleString());
    29     // 上午10:33:24
    30      console.log(newDate.toLocaleTimeString());
    31     // Wed Jun 18 2018 10:33:24 GMT+0800 (中国标准时间)
    32     console.log(newDate.toString());
    33     // 10:33:24 GMT+0800 (中国标准时间)
    34     console.log(newDate.toTimeString());
    35      // Wed, 18 Jun 2018 02:33:24 GMT
    36      console.log(newDate.toUTCString());
    37 Date.prototype.format = function(format) {
    38     var date = {
    39         "M+": this.getMonth() + 1,
    40         "d+": this.getDate(),
    41         "h+": this.getHours(),
    42         "m+": this.getMinutes(),
    43         "s+": this.getSeconds(),
    44         "q+": Math.floor((this.getMonth() + 3) / 3),
    45         "S+": this.getMilliseconds()
    46     };
    47     if (/(y+)/i.test(format)) {
    48             format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    49         }
    50     for (var k in date) {
    51        if (new RegExp("(" + k + ")").test(format)) {
    52             format = format.replace(RegExp.$1, RegExp.$1.length == 1
    53                 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    54        }
    55      }
    56      return format;
    57  }
    58  console.log(newDate.format('yyyy-MM-dd h:m:s'));
    59  </script>

    代码运行结果:

    JavaScrip(JS)t时间戳与时间日期间相互转换

  • 相关阅读:
    leetcode 62. Unique Paths
    leetcode 345. Reverse Vowels of a String
    leetcode 344. Reverse String
    Centos7.4 kafka集群安装与kafka-eagle1.3.9的安装
    yarn调度器 FairScheduler 与 CapacityScheduler
    Hive性能优化
    HBase笔记
    Zookeeper笔记
    Hadoop组件详解(随缘摸虾)
    ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建
  • 原文地址:https://www.cnblogs.com/nlyangtong/p/11043807.html
Copyright © 2011-2022 走看看