Skip to content

Commit

Permalink
Update date.js (#1937)
Browse files Browse the repository at this point in the history
替换被弃用的RegExp.$1、substr
  • Loading branch information
tesun authored Nov 16, 2024
1 parent f7889b9 commit b954457
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions web/src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
// eslint-disable-next-line no-extend-native
Date.prototype.Format = function(fmt) {
const o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
S: this.getMilliseconds() // 毫秒
'S': this.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
const reg = /(y+)/
if (reg.test(fmt)) {
const t = reg.exec(fmt)[1]
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + '').substr(4 - RegExp.$1.length)
t,
(this.getFullYear() + '').substring(4 - t.length)
)
}
for (const k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
for (let k in o) {
const regx = new RegExp('(' + k + ')')
if (regx.test(fmt)) {
const t = regx.exec(fmt)[1]
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
t,
t.length === 1 ? o[k] : ('00' + o[k]).substring(('' + o[k]).length)
)
}
}
Expand Down

0 comments on commit b954457

Please sign in to comment.