1234567891011121314151617181920212223242526 |
- export const formatTime = (t, format) => {
- if (typeof t === 'number' || typeof t === 'string') {
- t = new Date(t);
- }
- if (!(t instanceof Date)) {
- return null;
- }
- const o = {
- 'M+': t.getMonth() + 1,
- 'd+': t.getDate(),
- 'h+': t.getHours(),
- 'm+': t.getMinutes(),
- 's+': t.getSeconds(),
- 'q+': Math.floor((t.getMonth() + 3) / 3),
- 'S': t.getMilliseconds() // millisecond
- };
- if (/(y+)/.test(format)) {
- format = format.replace(RegExp.$1, (t.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
- for (const 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;
- };
|