合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
```js export function dateFormat(date) { let format = 'yyyy-MM-dd hh:mm:ss'; if (date != 'Invalid Date') { var o = { "M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+": date.getHours(), //hour "m+": date.getMinutes(), //minute "s+": date.getSeconds(), //second "q+": Math.floor((date.getMonth() + 3) / 3), //quarter "S": date.getMilliseconds() //millisecond } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; } return ''; } ``` ### 格式话 ```js export default function (date, fmt = 'YYYY-MM-DD HH:mm:ss') { var o = { 'M+': date.getMonth() + 1, 'D+': date.getDate(), 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, 'H+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds(), 'q+': Math.floor((date.getMonth() + 3) / 3), 'S': date.getMilliseconds() } var week = { '0': '\u65e5', '1': '\u4e00', '2': '\u4e8c', '3': '\u4e09', '4': '\u56db', '5': '\u4e94', '6': '\u516d' } if (/(Y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + '']) } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) } } return fmt } ``` ### 格式化 ``` Date.prototype.format = function (pattern) { let pad = function (source, length) { let pre = "", negative = (source < 0), string = String(Math.abs(source)); if (string.length < length) { pre = (new Array(length - string.length + 1)).join('0'); } return (negative ? "-" : "") + pre + string; }; if (typeof pattern !== 'string') { return this.toString(); } let replacer = function (patternPart, result) { pattern = pattern.replace(patternPart, result); }; let year = this.getFullYear(), month = this.getMonth() + 1, date2 = this.getDate(), hours = this.getHours(), minutes = this.getMinutes(), seconds = this.getSeconds(), milliSec = this.getMilliseconds(); replacer(/yyyy/g, pad(year, 4)); replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2)); replacer(/MM/g, pad(month, 2)); replacer(/M/g, month); replacer(/dd/g, pad(date2, 2)); replacer(/d/g, date2); replacer(/HH/g, pad(hours, 2)); replacer(/H/g, hours); replacer(/hh/g, pad(hours % 12, 2)); replacer(/h/g, hours % 12); replacer(/mm/g, pad(minutes, 2)); replacer(/m/g, minutes); replacer(/ss/g, pad(seconds, 2)); replacer(/s/g, seconds); replacer(/SSS/g, pad(milliSec, 3)); replacer(/S/g, milliSec); return pattern; }; ```