已經(jīng)自己寫了一個解析方法
/**
* 解析字符串為 Date 對象
* @param dateStr 日期字符串
* @param fmt 日期字符串的格式
* 目前僅支持使用 y(年),M(月),d(日),h(時),m(分),s(秒),S(毫秒)
*/
Date.of = function (dateStr, fmt) {
if (!dateStr) {
throw new Error('傳入的日期字符串不能為空!');
}
if (!fmt) {
throw new Error('傳入的日期字符串的自定義格式不能為空!');
}
/**
* 日期格式化對象
* @param name 日期格式的名稱
* @param format 日期的格式值
* @param value 格式化得到的值
* @constructor
*/
function DateFormat(name, format, value, index) {
this.name = name;
this.format = format;
this.value = value;
this.index = index;
}
//日期時間的正則表達式
const dateFormats = {
year: 'y{1,4}',
month: 'M{1,2}',
day: 'd{1,2}',
hour: 'h{1,2}',
minute: 'm{1,2}',
second: 's{1,2}',
milliSecond: 'S{1,3}'
};
//如果沒有格式化某項的話則設(shè)置為默認時間
const defaultDateValues = {
year: '2001',
month: '01',
day: '01',
hour: '00',
minute: '00',
second: '00',
milliSecond: '000'
};
//保存對傳入的日期字符串進行格式化的全部信息數(shù)組列表
const dateUnits = [];
for (const fmtName in dateFormats) {
const regExp = new RegExp(dateFormats[fmtName]);
if (regExp.test(fmt)) {
const matchStr = regExp.exec(fmt)[0];
const regexStr = String.fill('`', matchStr.length);
const index = fmt.indexOf(matchStr);
fmt = fmt.replaceAll(matchStr, regexStr);
dateUnits.push(new DateFormat(fmtName, String.fill('\\d', matchStr.length), null, index));
} else {
dateUnits.push(new DateFormat(fmtName, null, defaultDateValues[fmtName], -1));
}
}
//進行驗證是否真的是符合傳入格式的字符串
fmt = fmt.replaceAll('`', '\d');
if (!new RegExp(fmt).test(dateStr)) {
return null;
}
//進行一次排序, 依次對字符串進行截取
dateUnits.sort(function (a, b) {
return a.index - b.index;
});
for (var i = 0, length = dateUnits.length; i < length; i++) {
const format = dateUnits[i].format;
if (format == null) {
continue;
}
const matchDateUnit = new RegExp(format).exec(dateStr);
if (matchDateUnit !== null && matchDateUnit.length > 0) {
dateStr = dateStr.replace(matchDateUnit[0], '');
dateUnits[i].value = matchDateUnit[0];
}
}
//將截取完成的信息封裝成對象并格式化標(biāo)準的日期字符串
const obj = dateUnits.toObject(function (item) {
return {
key: item.name,
value: item.value
};
});
const date = '{year}-{month}-{day} {hour}:{minute}:{second}:{milliSecond}'.format(obj);
try {
return new Date(date);
} catch (e) {
return null;
}
};
//下面是上面的 Date.of() 使用的一些輔助方法
/**
* 替換所有匹配exp的字符串為指定字符串
* @param exp 被替換部分的正則
* @param newStr 替換成的字符串
*/
String.prototype.replaceAll = function (exp, newStr) {
return this.replace(new RegExp(exp, "gm"), newStr);
};
/**
* 原型:字符串格式化
* @param args 格式化參數(shù)值
*/
String.prototype.format = function (args) {
var result = this;
if (arguments.length < 1) {
return result;
}
var data = arguments; // 如果模板參數(shù)是數(shù)組
if (arguments.length === 1 && typeof (args) === "object") {
// 如果模板參數(shù)是對象
data = args;
}
for (var key in data) {
var value = data[key];
if (undefined !== value) {
result = result.replaceAll("\\{" + key + "\\}", value);
}
}
return result;
};
/**
* 為 js 的 String 添加填充字符串的靜態(tài)方法
* @param item 填充的元素
* @param length 填充的長度
* @returns {string} 填充得到的字符串
*/
String.fill = function (item, length) {
var result = '';
for (var i = 0; i < length; i++) {
result += item;
}
return result;
};
/**
* js 數(shù)組轉(zhuǎn)換為一個 Object 對象
* @param fn 轉(zhuǎn)換方法
* @returns {{}} 得到的 Object 對象
*/
Array.prototype.toObject = function (fn) {
const obj = {};
this.map(fn)
.forEach(function (item) {
obj[item.key] = item.value;
});
return obj;
};
嗯,順便也發(fā)了一篇 blog 呢
https://rxliuli.blogspot.com/...
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。