js时间戳转为日期格式

转自:http://wyoojune.blog.163.com/blog/static/57093325201131193650725/

这个在PHP+mssql(日期类型为datetime)+ajax的时候才能用到,js需要把时间戳转为为普通格式,一般的情况下可能用不到

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <script>     
  2. function getLocalTime(nS) {     
  3.    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
  4. }     
  5. alert(getLocalTime(1293072805));     
  6. </script>   

弹出:2010年12月23日 10:53

也可以用:

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <script>     
  2. function getLocalTime(nS) {     
  3.     return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}     
  4. alert(getLocalTime(1293072805));     
  5. </script>     

 

如果想弹出:2010-10-20 10:00:00这个格式的也好办

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <script>     
  2. function getLocalTime(nS) {     
  3.    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");      
  4. }     
  5. alert(getLocalTime(1177824835));     
  6. </script>  


另外我也參考了別的網站的一些東東,如下:

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. function   formatDate(now)   {     
  2.           var   year=now.getYear();     
  3.           var   month=now.getMonth()+1;     
  4.           var   date=now.getDate();     
  5.           var   hour=now.getHours();     
  6.           var   minute=now.getMinutes();     
  7.           var   second=now.getSeconds();     
  8.           return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
  9.           }     
  10.      
  11.           var   d=new   Date(1230999938);     
  12.           alert(formatDate(d));  

特別要提一下的是:

MySQL傳過來的數據,有可能是字符串,要把他們轉化為數字,數字也要*1000,因為JS里用的是毫秒數!我的如下:

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. var time_num = $("date",message).text();  
  2.         var time_num = parseInt(time_num);     //传回来的是个字符串  
  3.         var d = new Date(time_num*1000);       //這個很重要,要*1000  
  4.         var temp_time = formatDate(d);  


第一個例子做的就比較正規,什麽情況都考慮到了!


**********************************20170310

在js里面想得到现在的日期与时间,以及前一个小时的日期与时间,以下的代码正确,与之前的代码不同的地方在于,秒数前面增加了一个"0".

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //end by jianhong  
  2. function   formatDate(now)   {     
  3.     var   year=now.getFullYear();     
  4.     var   month=now.getMonth()+1;     
  5.     var   date=now.getDate();     
  6.     var   hour=now.getHours();     
  7.     var   minute=now.getMinutes();     
  8.     var   second=now.getSeconds();   
  9.     if(parseInt(second)<10){  
  10.         second = '0'+second;  
  11.     }  
  12.     return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;     
  13.  }     
  14.   
  15. var d1=new  Date();    
  16. var d2=new Date(d1.getTime()-1*60*60*1000);  
  17. alert(formatDate(d1));  
  18. alert(formatDate(d2));  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值