js 处理Json 时间带T 时间格式

来源:互联网 发布:linux 添加epel源 编辑:程序博客网 时间:2024/05/16 19:40






<body><p id="demo">Click the button to display the date and time as a string, using the ISO standard.</p><button onclick="myFunction()">Try it</button><script>function myFunction(){var dateee = new Date("2017-07-09T09:46:49.667").toJSON();var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'')     alert(date)  }</script>

http://blog.csdn.net/hj7jay/article/details/51433745

点击打开链接

对于后台传过来的json数据是带T时间格式的坑处理的一些做法总结

new Date(data[j].addtime).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')

仅针对这个问题来说,不需要那么大量的代码即可完成

方案1(适用于中国标准时间):

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var date = new Date(+new Date()+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'')  

方案2(只用于题目中所述的格式转换):

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var date = new Date().toLocaleString().replace(/[年月]/g,'-').replace(/[日上下午]/g,'');  

方案3(最准确但最麻烦,不推荐):

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var date = new Date();  
  2.   
  3. var result = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();  




















                                             
0 0
原创粉丝点击