json查询日期后 前台转换显示

对于在数据库存储的日期,如果我们想返回到前台显示的话  我们可以使用

spring mvc的@responseBody注解,然后可以通过jackson 在实体类通过@jsonFormat  定义日期的格式返回到前台中,但是这一次 修改的话 挺麻烦,所以就百度json日期格式转换日期正常格式,抱着试一试的样子  找到了  记录一下  也做以后的复习

 

需要注意的是:下面对jsonDate进行了一个replace的处理,这要看你传过来的日期是什么格式 

如果你的格式本身就是  date: 1231312318   这种  就不需要处理了  

需要处理的是传递过来如果是这种  /Date()/23131   日期这种格式的

 

  1. function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式  
  2.     try {  
  3.         var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));  
  4.         var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;  
  5.         var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();  
  6.         var hours = date.getHours();  
  7.         var minutes = date.getMinutes();  
  8.         var seconds = date.getSeconds();  
  9.         var milliseconds = date.getMilliseconds();  
  10.         return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;  
  11.     } catch (ex) {  
  12.         return "";  
  13.     }  
  14. }  

将查询的日期数据 传递到上面jsonDate里  进行下面的转换

然后表格种 就可以正常使用了  

贴几个当初搜索时  的链接地址

 

 

 

 

https://www.cnblogs.com/kissdodog/p/5419923.html

 

  1. function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式  
  2.     try {  
  3.         var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));  
  4.         var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;  
  5.         var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();  
  6.         var hours = date.getHours();  
  7.         var minutes = date.getMinutes();  
  8.         var seconds = date.getSeconds();  
  9.         var milliseconds = date.getMilliseconds();  
  10.         return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;  
  11.     } catch (ex) {  
  12.         return "";  
  13.     }  
  14. }  
posted @ 2018-04-14 17:35  溢性循环  阅读(85)  评论(0)    收藏  举报