Skip to content

fix(date): compute %U and %W week numbers like strftime - #958

Merged
harttle merged 1 commit into
harttle:masterfrom
web-orbit12:fix/strftime-week-number
Sep 27, 2026
Merged

harttle merged 1 commit into
harttle:masterfrom
web-orbit12:fix/strftime-week-number

Conversation

@web-orbit12

@web-orbit12 web-orbit12 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

%U and %W in the date filter return the wrong week number for a large share of dates.

{{ "2006-07-05" | date: "%U %W" }}
liquidjs -> "26 26"
Ruby     -> "27 27"

getWeekOfYear (src/util/strftime.ts:25) has two problems:

  • When the date's weekday comes before startDay (a Sunday with %W), "skip to startDay of this week" moves forward instead of back, so every Sunday is counted in the following %W week.
  • The first start day of the year is computed as 7 - jan1.getDay() + startDay, which lands a week too late when January 1st is itself the start day, so those years (2006 for %U, 2007 for %W) come out one week short.

This replaces it with the usual strftime formula, (yday + 7 - daysSinceStartDay) / 7 with a 0-based yday, which no longer depends on January 1st.

Verification

Measured against the liquid gem 5.14.0:

date weekday Ruby %U %W liquidjs before liquidjs after
2006-07-05 Wed 27 27 26 26 27 27
2000-01-02 Sun 01 00 01 01 01 00
2021-01-03 Sun 01 00 01 01 01 00
2007-01-01 Mon 00 01 00 00 00 01

Every day from 2000-01-01 to 2030-12-30 (11,322 dates) compared with strftime: before, %U was wrong on 1,461 dates and %W on 3,961; after, 0 with TZ=UTC, America/Los_Angeles and Pacific/Kiritimati.

TZ=Asia/Shanghai npm test (as in test.yml): 89 suites, 1671 tests, all passing. npm run lint exit 0. Reverting the change in strftime.ts fails the 3 new tests.

@harttle
harttle merged commit 0587ca8 into harttle:master Sep 27, 2026
15 checks passed
@harttle

harttle commented Sep 27, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @web-orbit12 for code

@allcontributors

Copy link
Copy Markdown
Contributor

@harttle

I've put up a pull request to add @web-orbit12! 🎉

github-actions Bot pushed a commit that referenced this pull request Oct 1, 2026
# [10.30.0](v10.29.0...v10.30.0) (2026-10-01)

### Bug Fixes

* **base64_encode:** encode large input in browser builds ([#969](#969)) ([8aace78](8aace78)), closes [#967](#967)
* **date:** compute %C and %y from the numeric year ([#961](#961)) ([e68514f](e68514f))
* **date:** compute %U and %W week numbers like strftime ([#958](#958)) ([0587ca8](0587ca8))
* trim custom characters by Unicode code point ([#955](#955)) ([3fe244f](3fe244f))
* **truncatewords:** ignore leading whitespace when counting words ([#954](#954)) ([65e200e](65e200e))
* **url_decode:** keep %2B as a literal plus when decoding ([#939](#939)) ([9af92f5](9af92f5))

### Features

* await promises returned by Drop.valueOf() ([#948](#948)) ([#953](#953)) ([#959](#959)) ([7dfa76b](7dfa76b))
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 10.30.0 🎉

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 this pull request may close these issues.

2 participants