1. 程式人生 > 實用技巧 >JS 時間格式轉換

JS 時間格式轉換

 1 /*
 2  * d  日期字串
 3  * type  D返回日期  T返回時間
 4  */
 5 export function dateTextFormat(d, type) {
 6     var dateTime = "";
 7     if (d != null) {
 8         if (d.length >= 8) {
 9             dateTime = d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8);
10             if (type == 'D') {
11 return dateTime; 12 } 13 } 14 if (d.length == 14) { 15 var time = d.substring(8, 10) + ":" + d.substring(10, 12) + ":" + d.substring(12, 14); 16 dateTime += " " + time; 17 if (type == 'T') { 18 return time;
19 } 20 21 } 22 } 23 return dateTime; 24 }

 1 /**
 2  * @date:日期時間戳
 3  * @format:需要轉換的格式:yyyy-MM-dd
 4  */
 5 export function dateFormat(date, format) {
 6     if (typeof date == "string") {
 7         return formatDate(new Date(parseInt(date)), format);
 8     } else {
9 return formatDate(new Date(date), format); 10 } 11 }