• javascript时间戳和日期字符串相互转换


    // 获取当前时间戳(以s为单位)
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    //当前时间戳为:1403149534
    console.log("当前时间戳为:" + timestamp);
    
    // 获取某个时间格式的时间戳
    var stringTime = "2014-07-10 10:21:12";
    var timestamp2 = Date.parse(new Date(stringTime));
    timestamp2 = timestamp2 / 1000;
    //2014-07-10 10:21:12的时间戳为:1404958872 
    console.log(stringTime + "的时间戳为:" + timestamp2);
    
    // 将当前时间换成时间格式字符串
    var timestamp3 = 1403058804;
    var newDate = new Date();
    newDate.setTime(timestamp3 * 1000);
    // Wed Jun 18 2014 
    console.log(newDate.toDateString());
    // Wed, 18 Jun 2014 02:33:24 GMT 
    console.log(newDate.toGMTString());
    // 2014-06-18T02:33:24.000Z
    console.log(newDate.toISOString());
    // 2014-06-18T02:33:24.000Z 
    console.log(newDate.toJSON());
    // 2014年6月18日 
    console.log(newDate.toLocaleDateString());
    // 2014年6月18日 上午10:33:24 
    console.log(newDate.toLocaleString());
    // 上午10:33:24 
    console.log(newDate.toLocaleTimeString());
    // Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
    console.log(newDate.toString());
    // 10:33:24 GMT+0800 (中国标准时间) 
    console.log(newDate.toTimeString());
    // Wed, 18 Jun 2014 02:33:24 GMT
    console.log(newDate.toUTCString());
    
    Date.prototype.format = function(format) {
           var date = {
                  "M+": this.getMonth() + 1,
                  "d+": this.getDate(),
                  "h+": this.getHours(),
                  "m+": this.getMinutes(),
                  "s+": this.getSeconds(),
                  "q+": Math.floor((this.getMonth() + 3) / 3),
                  "S+": this.getMilliseconds()
           };
           if (/(y+)/i.test(format)) {
                  format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
           }
           for (var k in date) {
                  if (new RegExp("(" + k + ")").test(format)) {
                         format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                  }
           }
           return format;
    }
    console.log(newDate.format('yyyy-MM-dd h:m:s'));
    
    </script>

    1、当前时间换时间戳

    
    
    var timestamp = parseInt(new Date().getTime()/1000);    // 当前时间戳
    document.write(timestamp);
    
    

     

    
    

    2、当前时间换日期字符串

    
    
    var now = new Date();
    var yy = now.getFullYear();      //var mm = now.getMonth() + 1;     //var dd = now.getDate();          //var hh = now.getHours();         //var ii = now.getMinutes();       //var ss = now.getSeconds();       //var clock = yy + "-";
    if(mm < 10) clock += "0";
    clock += mm + "-";
    if(dd < 10) clock += "0";
    clock += dd + " ";
    if(hh < 10) clock += "0";
    clock += hh + ":";
    if (ii < 10) clock += ‘0‘; 
    clock += ii + ":";
    if (ss < 10) clock += ‘0‘; 
    clock += ss;
    document.write(clock);     //获取当前日期
    
    

     

    
    

    3、日期字符串转时间戳

    
    
    var date = ‘2015-03-05 17:59:00.0‘;
    date = date.substring(0,19);    
    date = date.replace(/-/g,‘/‘); 
    var timestamp = new Date(date).getTime();
    document.write(timestamp);
    
    

     

    
    

    4、时间戳转日期字符串

    
    
    var timestamp = ‘1425553097‘;
    var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" +
               (d.getMonth() + 1) + "-" +
               (d.getDate()) + " " +
               (d.getHours()) + ":" +
               (d.getMinutes()) + ":" +
               (d.getSeconds());
    document.write(date);
    
    
    <script>     
    function getLocalTime(nS) {     
       return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');     
    }     
    alert(getLocalTime(1293072805));     
    </script> 
    
    
    复制代码
    
    
    
    
    

    结果是
    2010年12月23日 10:53
    第二种

    
    
    
    
    
    <script>     
    function getLocalTime(nS) {     
        return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}     
    alert(getLocalTime(1293072805));     
    </script> 
    
    
    
    
    

    如果你想得到这样格式的怎么办呢?
    2010-10-20 10:00:00
    看下面代码吧

    
    
    复制代码
     <script>     
        function getLocalTime(nS) {     
           return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");      
        }     
        alert(getLocalTime(1177824835));     
        </script>  
    复制代码
    
    

    也可以这样写的

    
    
    复制代码
    function   formatDate(now)   {     
                  var   year=now.getYear();     
                  var   month=now.getMonth()+1;     
                  var   date=now.getDate();     
                  var   hour=now.getHours();     
                  var   minute=now.getMinutes();     
                  var   second=now.getSeconds();     
                  return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
                  }     
             
                  var   d=new   Date(1230999938);     
                  alert(formatDate(d)); 
    复制代码
    
    

    好了问题解决
    需要注意的是
    不要把字符串中的Date(这样的字符也传进去,要先处理一下,这样很方便 就能处理的
    可以使用replace方法
    如下:

    
    
    replace("/Date(","").replace(")/","");
    
    

    JS 时间字符串与时间戳之间的转换

    
    
    时间:2015-03-05 20:46:54      阅读:2810      评论:0      收藏:0      [点我收藏+]
    
    

    标签:

    
    

    1、当前时间换时间戳

    
    
    var timestamp = parseInt(new Date().getTime()/1000);    // 当前时间戳
    document.write(timestamp);
    
    
    
    
    

    2、当前时间换日期字符串

    
    
    var now = new Date();
    var yy = now.getFullYear();      //var mm = now.getMonth() + 1;     //var dd = now.getDate();          //var hh = now.getHours();         //var ii = now.getMinutes();       //var ss = now.getSeconds();       //var clock = yy + "-";
    if(mm < 10) clock += "0";
    clock += mm + "-";
    if(dd < 10) clock += "0";
    clock += dd + " ";
    if(hh < 10) clock += "0";
    clock += hh + ":";
    if (ii < 10) clock += ‘0‘; 
    clock += ii + ":";
    if (ss < 10) clock += ‘0‘; 
    clock += ss;
    document.write(clock);     //获取当前日期
    
    
    
    
    

    3、日期字符串转时间戳

    
    
    var date = ‘2015-03-05 17:59:00.0‘;
    date = date.substring(0,19);    
    date = date.replace(/-/g,‘/‘); 
    var timestamp = new Date(date).getTime();
    document.write(timestamp);
    
    
    
    
    

    4、时间戳转日期字符串

    
    
    var timestamp = ‘1425553097‘;
    var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" +
               (d.getMonth() + 1) + "-" +
               (d.getDate()) + " " +
               (d.getHours()) + ":" +
               (d.getMinutes()) + ":" +
               (d.getSeconds());
    document.write(date);
     
  • 相关阅读:
    Memcached学习(一)
    了解下SoftReference
    Java Thread UncaughtExceptionHandler
    csdn 不登录浏览全文 chrome 浏览器
    postgresql 主从 patroni
    熔断,限流,降级
    CentOS7.4 源码安装MySQL8.0
    CentOS BIND9安装及配置
    Linux安全之SYN攻击原理及其应对措施
    fping常用参数介绍
  • 原文地址:https://www.cnblogs.com/try-better-tomorrow/p/5050583.html
Copyright © 2020-2023  润新知