UNPKG

fengcheng-time

Version:

102 lines (93 loc) 3.93 kB
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <meta name="format-detection" content="telephone=no"> <title></title> <script src="../index.js"></script> </head> <body> <script type="text/javascript"> var t1 = TIME.locale({ lang : "zh-cn", format : { LT : 'HH:mm', LTS : 'HH:mm:ss', L : 'YYYY-MM-DD', LL : 'YYYY-MM-DD HH:mm', }, parseDateString : function(dateString){ // 2018-05-11 10:01:02 // 2018-05-19 var arr = dateString.split(" "); var match = null; var result = {}; if(arr.length > 2) throw "不支持的时间类型"; if(arr.length === 2){ // 2018-05-19 10:01:02 match = dateString.match(/(\d{4})?[^\d]*(\d*)[^\d]*(\d*)\s*(\d*):?(\d*):?(\d*)/); if(match[1]) result.year = Number(match[1]); if(match[2]) result.month = Number(match[2]); if(match[3]) result.day = Number(match[3]); if(match[4]) result.hour = Number(match[4]); if(match[5]) result.minute = Number(match[5]); if(match[6]) result.second = Number(match[6]); } else if(arr[0].search(":") === -1){ // 2018-05-19 match = dateString.match(/(\d*)[^\d]*(\d*)[^\d]*(\d*)/); if(match[1]) result.year = Number(match[1]); if(match[2]) result.month = Number(match[2]); if(match[3]) result.day = Number(match[3]); } else{ // 10:05 match = dateString.match(/(\d*):?(\d*):?(\d*)/); if(match[1]) result.hour = Number(match[1]); if(match[2]) result.minute = Number(match[2]); if(match[3]) result.second = Number(match[3]); } return result; } }); var t2 = TIME.locale({ lang : "en", months : ["Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"], format : { LT : 'HH:mm', LTS : 'HH:mm:ss', L : '%M D, YYYY', LL : '%M D, YYYY HH:mm', }, parseFormatString : { "%M" : function(){ return this.config.months[this.month - 1]; } }, parseDateString : function(dateString){ // May 19, 2018 18:01 // May 19, 2018 var match = dateString.match(/([a-zA-Z]+)[^\d]+(\d+)[^\d]+(\d+)[^\d]*(\d*):*(\d*):*(\d*)/); var result = {}; if(match[1]) result.month = this.config.months.indexOf(match[1]) + 1; if(match[2]) result.day = Number(match[2]); if(match[3]) result.year = Number(match[3]); if(match[4]) result.hour = Number(match[4]); if(match[5]) result.minute = Number(match[5]); if(match[6]) result.second = Number(match[6]); return result; } }); var timestamp = t1("2018-05-10 10:13").getTimestamp(); // 转换为本地时区时间戳 console.log(t1(timestamp).format("LL") === "2018-05-10 10:13"); var timestamp2 = t1("2018-05-10 10:13").getTimestamp(0); // 转换为GMT+0时区时间戳 console.log(t1(timestamp2, 0).format("LL") === "2018-05-10 10:13"); var timestamp3 = t1(timestamp2, 0).getTimestamp(8); // GMT+0时区时间戳转换为GMT+8时区时间戳 console.log(t1(timestamp3, 8).format("LL") === "2018-05-10 10:13"); var timestamp4 = t2("May 10, 2018 10:12").getTimestamp(); // 转换为本地时区时间戳 console.log(t2(timestamp4).format("LL") === "May 10, 2018 10:12"); console.log(t2(timestamp4, 0).format("LL") === "May 10, 2018 18:12"); // GMT+0时区时间戳转换成本地时间 </script> </body> </html>