zoukankan      html  css  js  c++  java
  • JavaScript获取时间戳与时间戳转化

    第一种方法(精确到秒):

    var timestamp1 = Date.parse( new Date());
    

    第二种方法(精确到毫秒): 

    var timestamp2 = ( new Date()).valueOf();
    

    第三种方法(精确到毫秒):

    var timestamp3 = new Date().getTime();
    

    获取指定时间的时间戳: 

    new Date("2018-09-11 20:45:00");
    

    时间戳转化成时间: 

      //第一种
      function getLocalTime(nS) {     
         return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');     
      }     
      alert(getLocalTime(1536670500));
      //结果是2018年09月11日 20:55
      //第二种    
      function getLocalTime(nS) {     
          return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
     }     
     alert(getLocalTime(1536670515));
     //第三种  格式为:2018-09-11 20:55:15
      function getLocalTime(nS) {     
            return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");      
         }     
     alert(getLocalTime(1536670515));
    //第四种
     function timetrans(date){
        var date = new Date(date*1000);//如果date为13位不需要乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
    }
    

      

     

     

     

      

     

  • 相关阅读:
    larbin结构分析
    《钱不要存银行》OneNote
    全局变量、extern/static/const区别与联系
    GIS网址,转自别处
    MSDN无法显示页面的解决
    人生没有奇迹
    开源GIS系统
    推荐:GDAL学习资源
    中国农科院资源区划所MODIS的遥感信息地面接收站
    泡沫产生的特点
  • 原文地址:https://www.cnblogs.com/zhaojunhao/p/9630315.html
Copyright © 2011-2022 走看看