JavaScript 格式化时间

 JavaScript接收的json字符串中时间往往不是正常时间,而是一串数字。可用下列方法格式化为正常时间。

 1 //格式化 yyyy-MM-dd hh:mm:ss
 2 function renderTime(date) {
 3     if (date == '' || date == null) {
 4         return '';
 5     }
 6     var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
 7     return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate() + " " + da.getHours() + ":" + da.getMinutes() + ":" + da.getSeconds();
 8 }
 9 
10 //格式化 yyyy-0M-0d
11 function renderDate(date) {
12     if (date == '' || date == null) {
13         return '';
14     }
15     var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
16     return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate();
17 }
18 
19 //格式化时间  yyyy-mm-dd
20 function getFormatDate(date) {
21 
22     if (date == '' || date == null) {
23         return '';
24     }
25     var day = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
26     var Year = 0;
27     var Month = 0;
28     var Day = 0;
29     var CurrentDate = "";
30     //初始化时间 
31     //Year= day.getYear();//有火狐下2008年显示108的bug 
32     Year = day.getFullYear();//ie火狐下都可以 
33     Month = day.getMonth() + 1;
34     Day = day.getDate();
35     //Hour = day.getHours(); 
36     // Minute = day.getMinutes(); 
37     // Second = day.getSeconds(); 
38     CurrentDate += Year + "-";
39     if (Month >= 10) {
40         CurrentDate += Month + "-";
41     }
42     else {
43         CurrentDate += "0" + Month + "-";
44     }
45     if (Day >= 10) {
46         CurrentDate += Day;
47     }
48     else {
49         CurrentDate += "0" + Day;
50     }
51     return CurrentDate;
52 }

 

posted @ 2016-01-08 11:54  gaozejie  阅读(333)  评论(0编辑  收藏  举报