1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
Date.of = function (dateStr, fmt) { if (!dateStr) { throw new Error('传入的日期字符串不能为空!') } if (!fmt) { throw new Error('传入的日期字符串的自定义格式不能为空!') }
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}', } const defaultDateValues = { year: '2001', month: '01', day: '01', hour: '00', minute: '00', second: '00', milliSecond: '000', } 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] } } const obj = dateUnits.toObject(function (item) { return { key: item.name, value: item.value, } }) const date = '{year}-{month}-{day}T{hour}:{minute}:{second}.{milliSecond}'.format( obj, ) try { return new Date(date) } catch (e) { return null } }
String.prototype.replaceAll = function (exp, newStr) { return this.replace(new RegExp(exp, 'gm'), newStr) }
String.prototype.format = function (args) { var result = this if (arguments.length < 1) { return result }
var data = arguments if (arguments.length === 1 && typeof args === 'object') { data = args } for (var key in data) { var value = data[key] if (undefined !== value) { result = result.replaceAll('\\{' + key + '\\}', value) } } return result }
String.fill = function (item, length) { var result = '' for (var i = 0; i < length; i++) { result += item } return result }
Array.prototype.toObject = function (fn) { const obj = {} this.map(fn).forEach(function (item) { obj[item.key] = item.value }) return obj }
|