UNPKG

yzf-date-format

Version:

日期格式化

194 lines (189 loc) 9.55 kB
const DFormat = (value, format, k) => { // 日期Filter const Str = value; const ZeorFn = (a) => { let b; if (a < 10) { b = `0${a}`; } else { b = `${a}`; } return b; }; try{ let oDate; let onoff = false; if (Str) { oDate = new Date(Str); } else { oDate = new Date(); } const year = oDate.getFullYear(); const month = oDate.getMonth() + 1; const date = oDate.getDate(); const Hours = oDate.getHours(); const Minutes = oDate.getMinutes(); const Seconds = oDate.getSeconds(); if (!format) { return `${year}-${ZeorFn(month)}-${ZeorFn(date)} ` + `${ZeorFn(Hours)}:${ZeorFn(Minutes)}:${ZeorFn(Seconds)}`; } let DF = []; if (Object.prototype.toString.call(format) === '[object Array]') { DF = format; } else { DF = format.split(' '); // toLowerCase() if (k) { DF = format.split(k); // toLowerCase() } } let Y = DF[0] ? DF[0].toLowerCase() : null; let H = DF[1] ? DF[1].toLowerCase() : null; let strY = ''; if (Y) { onoff = true; const ZEY = /([^ymd]+)?(y{4})([^ymd]+)?/; const $y = Y.match(ZEY); const $yK = $y ? $y.index : 100; const ZEM = /([^ymd]+)?(m{1,2})([^ymd]+)?/; const $m = Y.match(ZEM); const $mK = $m ? $m.index : 100; const ZED = /([^ymd]+)?(d{1,2})([^ymd]+)?/; const $d = Y.match(ZED); const $dK = $d ? $d.index : 100; if ($yK <= $mK && $mK <= $dK) { // 年 月 日 strY += `${$y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; strY += `${!$y && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; strY += `${!$y && !$m && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; } else if ($yK <= $dK && $dK <= $mK) { // 年 日 月 strY += `${$y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; strY += `${!$y && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; strY += `${!$y && !$d && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; } else if ($mK <= $dK && $dK <= $yK) { // 月 日 年 strY += `${$m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; strY += `${!$m && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; strY += `${!$m && !$d && $y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; } else if ($mK <= $yK && $yK <= $dK) { // 月 年 日 strY += `${$m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; strY += `${!$m && $y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; strY += `${!$m && !$y && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; } else if ($dK <= $mK && $dK <= $yK) { // 日 月 年 strY += `${$d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; strY += `${!$d && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; strY += `${!$d && !$m && y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; } else if ($dK <= $yK && $yK <= $mK) { // 日 年 月 strY += `${$d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(date) : date) : ''}${$d && $d[3] ? $d[3] : ''}`; strY += `${!$d && y && $y[1] ? $y[1] : ''}${$y ? year : ''}${$y && $y[3] ? $y[3] : ''}`; strY += `${!$d && !$y && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(month) : month) : ''}${$m && $m[3] ? $m[3] : ''}`; } } let strH = ''; if (H) { onoff = true; const ZEY = /([^hms]+)?(h{1,2})([^hms]+)?/; const $y = H.match(ZEY); const $yK = $y ? $y.index : 100; const ZEM = /([^hms]+)?(m{1,2})([^hms]+)?/; const $m = H.match(ZEM); const $mK = $m ? $m.index : 100; const ZED = /([^hms]+)?(s{1,2})([^hms]+)?/; const $d = H.match(ZED); const $dK = $d ? $d.index : 100; if ($yK <= $mK && $mK <= $dK) { // 年 月 日 strH += `${$y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; strH += `${!$y && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; strH += `${!$y && !$m && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; } else if ($yK <= $dK && $dK <= $mK) { // 年 日 月 strH += `${$y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; strH += `${!$y && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; strH += `${!$y && !$d && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; } else if ($mK <= $dK && $dK <= $yK) { // 月 日 年 strH += `${$m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; strH += `${!$m && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; strH += `${!$m && !$d && $y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; } else if ($mK <= $yK && $yK <= $dK) { // 月 年 日 strH += `${$m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; strH += `${!$m && $y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; strH += `${!$m && !$y && $d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; } else if ($dK <= $mK && $dK <= $yK) { // 日 月 年 strH += `${$d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; strH += `${!$d && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; strH += `${!$d && !$m && y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; } else if ($dK <= $yK && $yK <= $mK) { // 日 年 月 strH += `${$d && $d[1] ? $d[1] : ''}${$d && $d[2] ? ($d[2].length > 1 ? ZeorFn(Seconds) : Seconds) : ''}${$d && $d[3] ? $d[3] : ''}`; strH += `${!$d && y && $y[1] ? $y[1] : ''}${$y && $y[2] ? ($y[2].length > 1 ? ZeorFn(Hours) : Hours) : ''}${$y && $y[3] ? $y[3] : ''}`; strH += `${!$d && !$y && $m && $m[1] ? $m[1] : ''}${$m && $m[2] ? ($m[2].length > 1 ? ZeorFn(Minutes) : Minutes) : ''}${$m && $m[3] ? $m[3] : ''}`; } } // 没匹配到 if (!onoff) { console.log(`暂时没有${format ? format : '该'}模式;如需添加请联系:yzflhez@126.com`); return `${year}-${ZeorFn(month)}-${ZeorFn(date)} ` + `${ZeorFn(Hours)}:${ZeorFn(Minutes)}:${ZeorFn(Seconds)}`; } // if (strY && !strH) { // return `${strY}${k ? k : ''}`; // } else if (!strY && strH) { // return `${k ? k : ''}${strH}`; // } if (!strY || !strH) { return `${strY ? strY : ''}${k ? k : ''}${strH ? strH : ''}`; } return `${strY}${k ? k : ' '}${strH}`; } catch (err) { // alert('代码出错请联系:yzflhez@126.com') return value } }; const AddDate = (value, toDate) => { let oDate; if (value) { oDate = new Date(value); } else { oDate = new Date(); } if (toDate && parseFloat(toDate).toString() != "NaN") { const Time = oDate.getTime(); oDate = new Date(Time + (86400000 * toDate)); } return new Date(oDate) } const AddMonth = (value, toMonth) => { let oDate; if (value) { oDate = new Date(value); } else { oDate = new Date(); } const ZeorFn = (a) => { let b; if (a < 10) { b = `0${a}`; } else { b = `${a}`; } return b; }; const year = oDate.getFullYear(); let month = oDate.getMonth() + 1; const date = oDate.getDate(); const Hours = oDate.getHours(); const Minutes = oDate.getMinutes(); const Seconds = oDate.getSeconds(); const NewMonth = parseFloat(toMonth); if (toMonth && NewMonth.toString() != "NaN") { let a = Math.floor(NewMonth / 12); let b = Math.floor(NewMonth % 12); month = month + b; if (month > 12) { a += 1; month = month - 12; } const NowTime = `${parseInt(year) + a}-${ZeorFn(month)}-${ZeorFn(date)} ` + `${ZeorFn(Hours)}:${ZeorFn(Minutes)}:${ZeorFn(Seconds)}`; return new Date(NowTime); } return new Date(oDate) } module.exports = { DFormat, AddDate, AddMonth };