fix(utils): parse relative date with meridiem (#9775)

* fix(utils): parse relative date with meridiem

* fix: use regex
This commit is contained in:
Tony
2022-05-17 01:21:47 -11:00
committed by GitHub
parent 731c11176d
commit 0f31bfa8b9
2 changed files with 8 additions and 2 deletions

View File

@@ -129,7 +129,7 @@ module.exports = {
// 将 `\d+年\d+月...\d+秒前` 分割成 `['\d+年', ..., '\d+秒前']`
const matches = theDate.match(/(?:\D+)?\d+(?!:|-|\/)\D+/g);
const matches = theDate.match(/(?:\D+)?\d+(?!:|-|\/|(a|p)m)\D+/g);
if (matches) {
// 获得最后的时间单元,如 `\d+秒前`
@@ -187,7 +187,9 @@ module.exports = {
for (const w of words) {
const wordMatches = w.regExp.exec(theDate);
if (wordMatches) {
return dayjs(`${w.startAt.format('YYYY-MM-DD')} ${wordMatches[1]}`).toDate();
// The default parser of dayjs() can parse '8:00 pm' but not '8:00pm'
// so we need to insert a space in between
return dayjs(`${w.startAt.format('YYYY-MM-DD')} ${/a|pm$/.test(wordMatches[1]) ? wordMatches[1].replace(/a|pm/, ' $&') : wordMatches[1]}`).toDate();
}
}
}