UNPKG

fengcheng-time

Version:

209 lines (190 loc) 8.87 kB
/* 时间转换 cheng.feng 20150511 国际化 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时区时间戳转换成本地时间 */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.TIME = factory()); }(this, function () { 'use strict'; var TIME = { locale : function(config){ return createInstance(config); } }; var Time = function(time, GMT, config){ this.config = config; if(typeof time === "number" || time === undefined || time === null){ this._transformTimeStamp(time, GMT); } else if(typeof time === "string"){ this._transformDateString(time, GMT); } else { throw "unknow " + time; } }; Time.prototype = { // timestamp 时间戳 // GMT 指定timestamp时间戳是哪个时区,默认本地时区 _transformTimeStamp : function(timestamp, GMT){ var GTMDate = timestamp ? new Date(timestamp) : new Date(); var GMTOffset = GTMDate.getTimezoneOffset()/60; if(typeof GMT !== "number") GMT = -GMTOffset; this.GMT = -GMTOffset; this.timestamp = GTMDate.getTime() - (GMTOffset + GMT) * 60*60*1000; // 得到本地时间 this._transformDate(this.timestamp); return this; }, _transformDateString : function(dateString, GMT){ dateString = dateString.trim(); var localeDate = new Date(); var GMTOffset = localeDate.getTimezoneOffset()/60; if(typeof GMT !== "number") GMT = -GMTOffset; var timestamp = localeDate.getTime() - (GMTOffset + GMT) * 60*60*1000; var GMTDate = new Date(timestamp); var parseDate = this.config.parseDateString.call(this, dateString); if(!parseDate.year) parseDate.year = GMTDate.getFullYear(); if(!parseDate.month) parseDate.month = GMTDate.getMinutes() + 1; if(!parseDate.day) parseDate.day = GMTDate.getDate(); if(!parseDate.hour) parseDate.hour = 0; if(!parseDate.minute) parseDate.minute = 0; if(!parseDate.second) parseDate.second = 0; this.timestamp = new Date(parseDate.year, parseDate.month-1, parseDate.day, parseDate.hour, parseDate.minute, parseDate.second).getTime() - (GMTOffset + GMT) * 60*60*1000; // 得到本地时间 this.GMT = -GMTOffset; this._transformDate(this.timestamp); this.dateString = dateString; return this; }, _transformDate : function(timestamp){ this.date = new Date(timestamp); this.year = this.date.getFullYear(); this.month = this.date.getMonth() + 1; this.day = this.date.getDate(); this.hour = this.date.getHours(); this.minute = this.date.getMinutes(); this.second = this.date.getSeconds(); }, format : function(flag){ if(!flag) flag = "L"; if(this.config.format[flag]){ return this._parseFormatString(this.config.format[flag]); } }, getTimestamp : function(GMT){ if(typeof GMT !== "number") GMT = this.GMT; var GMTOffset = this.GMT - GMT; return this.timestamp - GMTOffset*60*60*1000; }, _parseFormatString : function(format){ var that = this; var parseFormatString = this.config.parseFormatString; var reg = "YYYY|MM|M|DD|D|HH|mm|ss"; if(parseFormatString){ for(var key in parseFormatString) reg = key + "|" + reg; } return format.replace(new RegExp(reg, "g"), function(part){ if(parseFormatString && parseFormatString[part]){ return parseFormatString[part].call(that); } switch(part){ case "YYYY" : return that.year; case "MM" : return that.month < 10 ? "0" + that.month : that.month; case "M" : return that.month; case "DD" : return that.day < 10 ? "0" + that.day : that.day; case "D" : return that.day; case "HH" : return that.hour < 10 ? "0" + that.hour : that.hour; case "mm" : return that.minute < 10 ? "0" + that.minute : that.minute; case "ss" : return that.second < 10 ? "0" + that.second : that.second; } }); }, constructor : Time, }; var createInstance = function(config){ return function(timestamp, GMT){ if(timestamp instanceof Time) return timestamp; var instance = new Time(timestamp, GMT, config); return instance; }; }; return TIME; }))