zoukankan      html  css  js  c++  java
  • js时间戳怎么转成日期格式

    原文地址:http://www.sufeinet.com/thread-1500-1-1.html

    js时间戳怎么转成日期格式
    这个在主群里有朋友§☆釺哖蟲...o问js时间戳怎么转成日期格式 ,他的问题是这样的
    /Date(1354116249000)/ 这样的格式怎么转成时间格式
    这是从C#的Datatime格式通过Json传到Js里面的,
    下面是我们的提供的方法
    js需要把时间戳转为为普通格式,一般的情况下可能用不到的,
    下面先来看第一种吧

    <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(")/","");

     

    本人的博客不再维护从2013年就不再维护了 需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!
  • 相关阅读:
    编译内核开始的小问题Unable to find the Ncurses libraries
    qq for linux Ubuntu 64位兼容
    ubuntu下安装lighttpd
    搭建boa服务器
    INI file and Iniparser
    kernel常用.gitignore配置
    光谱学习
    jump to case label fpermissive
    Qt通用方法及类库5
    Qt通用方法及类库1
  • 原文地址:https://www.cnblogs.com/sufei/p/2799480.html
Copyright © 2011-2022 走看看