Skip to content

* helpers: parse date-only strings as local midnight - #247

Merged
catouse merged 1 commit into
easysoft:mainfrom
dalexhu:fix/create-date-local-midnight
Sep 5, 2026
Merged

* helpers: parse date-only strings as local midnight#247
catouse merged 1 commit into
easysoft:mainfrom
dalexhu:fix/create-date-local-midnight

Conversation

@dalexhu

@dalexhu dalexhu commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

问题

createDate('2026-09-17') 把纯日期字符串直接交给 Date 构造函数,而按 ECMAScript 规范,YYYY-MM-DD 这种只有日期的 ISO 串会被当作 UTC 零点。在 UTC 以西的时区里这就是本地的前一天:

// 浏览器时区 America/Edmonton (UTC-6)
zui.createDate('2026-09-17')                    // Wed Sep 16 2026 18:00:00 GMT-0600
zui.formatDate('2026-09-17', 'yyyy-MM-dd')     // "2026-09-16"

实际表现是日期选择器点 17 号,输入框里变成 16 号(在禅道 22.5 上复现,浏览器位于加拿大 Edmonton)。在 UTC+8 看不到这个问题,所以一直没被发现。

修改

  • createDate:匹配 ^\d{4}-\d{2}-\d{2}$ 的字符串改为按本地零点构造(new Date(year, month - 1, day)),与 YYYY-MM-DD HH:mm:ss 已有的本地时间语义一致。带时间的字符串、时间戳、Date 对象的行为不变。
  • 单元测试:在 America/EdmontonAsia/ShanghaiUTC 三个时区下验证纯日期串得到当天零点,并覆盖带 Z 的 ISO 串和带时间的字符串不受影响。未修复时该用例在 Edmonton 时区失败(得到 [2026, 8, 16, 18]),修复后通过。
  • 文档:date-helper.md 增加一条提示说明纯日期串按本地时区解析。

验证

  • pnpm exec vitest run tests/unit/helpers.test.ts:15 passed
  • pnpm exec eslint 对改动文件无告警;pnpm typecheck 通过

Summary (EN): createDate() passed date-only ISO strings to the Date constructor, which parses them as UTC midnight; in time zones west of UTC that is the previous local day, so date pickers showed the day before the one the user picked. Parse YYYY-MM-DD as local midnight instead; strings with a time part, timestamps and Date objects are unchanged. Adds a multi-time-zone unit test and a docs note.

🤖 Generated with Claude Code

`createDate('2026-09-17')` handed the string straight to the Date constructor,
which treats a date-only ISO string as UTC midnight. In any time zone west of
UTC that is the previous local day, so `formatDate('2026-09-17', 'yyyy-MM-dd')`
returned `2026-09-16` and the date picker showed the day before the one the
user clicked (reproduced in ZenTao 22.5 with a browser in America/Edmonton).

Parse `YYYY-MM-DD` as local midnight, the same way `YYYY-MM-DD HH:mm:ss` is
already handled. Strings with a time part, timestamps and Date objects are
unchanged. Adds a unit test that runs in several time zones and a note in the
helper docs.
@dalexhu
dalexhu force-pushed the fix/create-date-local-midnight branch from 5059ee4 to 5861f06 Compare September 5, 2026 03:37
@catouse
catouse merged commit 8c7e2b2 into easysoft:main Sep 5, 2026
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants