Skip to content

Commit

Permalink
fix(star-space):修复搜索结果时间格式不正确 (#1974)
Browse files Browse the repository at this point in the history
* fix(star-space):修复搜索结果时间格式不正确

* 修改时间函数
  • Loading branch information
SHL-COOL authored Aug 31, 2024
1 parent 001fed2 commit 2a4e9af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resource/sites/star-space.net/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"#tm_td_uploader"
],
"filters": [
"query.contents().filter(function() {return this.nodeType === Node.TEXT_NODE || (this.nodeType === Node.ELEMENT_NODE && this.tagName !== 'SPAN');}).text().trim()"
"query.text().parseTime()"
]
},
"size": {
Expand Down
31 changes: 31 additions & 0 deletions src/interface/types.expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,34 @@ String.prototype.timeToDays = function() {
})
return length;
};



String.prototype.parseTime = function() {
const timeMatch = this.match(/\d+[分时天月年]/g)
let length = 0
timeMatch.forEach(time => {
const timeMatch = time.match(/(\d+)([分时天月年])/)
const number = parseInt(timeMatch[1])
const unit = timeMatch[2]
switch (true) {
case unit === '分':
length += number
break
case unit === '时':
length += number * 60
break
case unit === '天':
length += number * 60 * 24
break
case unit === '月':
length += number * 60 * 24 * 30
break
case unit === '年':
length += number * 60 * 24 * 365
break
default:
}
})
return new Date(Date.now() - length * 60 * 1000).toLocaleString("zh-CN", { hour12: false }).replace(/\//g, '-')
}

0 comments on commit 2a4e9af

Please sign in to comment.