chore(utils): parse-date supports relative time & fix routes (#7530)

This commit is contained in:
Queensferry
2021-05-15 11:08:33 +08:00
committed by GitHub
parent 2bc0d12019
commit 65e74a1c5e
13 changed files with 66 additions and 18 deletions

View File

@@ -1,4 +1,42 @@
const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/customParseFormat'));
module.exports = (date, ...options) => dayjs(date, ...options).toDate();
const patterns = [
{
regexp: /^(\d+)分钟前$/,
handler: (minute) => dayjs().subtract(minute, 'minutes'),
},
{
regexp: /^(\d+)小时前$/,
handler: (hour) => dayjs().subtract(hour, 'hours'),
},
{
regexp: /^(\d+)天前$/,
handler: (day) => dayjs().subtract(day, 'days'),
},
{
regexp: /^今天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']),
},
{
regexp: /^昨天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']).subtract(1, 'day'),
},
{
regexp: /^前天\s*((\d+:\d+)?)$/,
handler: (hm) => dayjs(hm || '0:0', ['HH:m', 'HH:mm', 'H:m', 'H:mm']).subtract(2, 'day'),
},
];
module.exports = {
parseDate: (date, ...options) => dayjs(date, ...options).toDate(),
parseRelativeDate: (date) => {
for (const pattern of patterns) {
const match = pattern.regexp.exec(date);
if (match !== null) {
return pattern.handler(match[1]).toDate();
}
}
return null;
},
};