Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dayjs.diff 错误 #2428

Closed
gee1k opened this issue Aug 25, 2023 · 3 comments · Fixed by #2429
Closed

dayjs.diff 错误 #2428

gee1k opened this issue Aug 25, 2023 · 3 comments · Fixed by #2429
Labels

Comments

@gee1k
Copy link
Contributor

gee1k commented Aug 25, 2023

Describe the bug
A clear and concise description of what the bug is.

ERROR
Cannot read properties of undefined (reading '$localOffset')

Expected behavior

通过调试发现这种情况下 endTime对象内部没有 $x 属性,所以导致,获取 $x.$localOffset 时报错

const timezoneOffset = 8
const endTime = dayjs('2023/08/25 10:35', 'YYYY/MM/DD HH:mm').utcOffset(8, true)
const now = dayjs().utcOffset(timezoneOffset)

const diff = endTime.diff(now, 'second')

但是只需要改成下面这样,去掉 dayjs 的时间格式,endTime对象就会有一个为空对象{}$x属性。就能正常使用

const timezoneOffset = 8
const endTime = dayjs('2023/08/25 10:35').utcOffset(8, true)
const now = dayjs().utcOffset(timezoneOffset)

const diff = endTime.diff(now, 'second')

Information

  • Day.js Version v1.11.9
  • OS: macOS
  • Browser chrome 116
  • Time zone: GMT+8:00
@gee1k
Copy link
Contributor Author

gee1k commented Aug 25, 2023

测试发现和utcOffset没有关系,下面这样就会有差别

const endTime = dayjs('2023/08/25 10:35')
const endTime2 = dayjs('2023/08/25 10:35', 'YYYY/MM/DD HH:mm')

@gee1k
Copy link
Contributor Author

gee1k commented Aug 25, 2023

原因应该是在这里,当设置了自定义 format 的时候,进入了 customParseFormat插件,插件里的 parse 方法在 format 有值的时候替换了 dayjs 原有的 parse,但是没有像原有 parse 方法一样,给 $x 赋值

proto.parse = function (cfg) {
const {
date,
utc,
args
} = cfg
this.$u = utc
const format = args[1]
if (typeof format === 'string') {
const isStrictWithoutLocale = args[2] === true
const isStrictWithLocale = args[3] === true
const isStrict = isStrictWithoutLocale || isStrictWithLocale
let pl = args[2]
if (isStrictWithLocale) [,, pl] = args
locale = this.$locale()
if (!isStrictWithoutLocale && pl) {
locale = d.Ls[pl]
}
this.$d = parseFormattedInput(date, format, utc)
this.init()
if (pl && pl !== true) this.$L = this.locale(pl).$L
// use != to treat
// input number 1410715640579 and format string '1410715640579' equal
// eslint-disable-next-line eqeqeq
if (isStrict && date != this.format(format)) {
this.$d = new Date('')
}
// reset global locale to make parallel unit test
locale = {}
} else if (format instanceof Array) {
const len = format.length
for (let i = 1; i <= len; i += 1) {
args[1] = format[i - 1]
const result = d.apply(this, args)
if (result.isValid()) {
this.$d = result.$d
this.$L = result.$L
this.init()
break
}
if (i === len) this.$d = new Date('')
}
} else {
oldParse.call(this, cfg)
}
}

gee1k added a commit to gee1k/dayjs that referenced this issue Aug 25, 2023
@github-actions
Copy link

🎉 This issue has been resolved in version 1.11.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant