Zhaoxin Guo

Don't lose hope. You never know what tomorrow will bring.

Json序列化日期/Date(xxxx)/ JS转化为常用日期格式

记录开发过程中的代码片段,方便日后归纳、总结,效果如图所示:

转换前:   

转换后:

 

代码如下,需要的朋友们自取:

 1 //JS转化为json常用日期格式
 2         function FormatToDate(val) {
 3             if (val != null) {
 4                 var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
 5                 //月份为0-11,所以+1,月份小于10时补个0
 6                 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
 7                 var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
 8                 return date.getFullYear() + "-" + month + "-" + currentDate;
 9             }
10             return "";
11         }

 

 

 

posted @ 2019-11-18 15:32  "郭召欣"  阅读(2297)  评论(0编辑  收藏  举报