Skip to content

Null date gets converted to Jan 1, 1970 #868

Closed

Description

@timbze

Looking at the parseDate function I think null falls into the final else block and new Date(null) does return 1970-01-01.

To fix could simply add one more if (isNil(v)) return undefined check before the existing if checks.

function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffset?: number | string): LiquidDate | undefined {
let date: LiquidDate | undefined
const defaultTimezoneOffset = timezoneOffset ?? opts.timezoneOffset
const locale = opts.locale
v = toValue(v)
if (v === 'now' || v === 'today') {
date = new LiquidDate(Date.now(), locale, defaultTimezoneOffset)
} else if (isNumber(v)) {
date = new LiquidDate(v * 1000, locale, defaultTimezoneOffset)
} else if (isString(v)) {
if (/^\d+$/.test(v)) {
date = new LiquidDate(+v * 1000, locale, defaultTimezoneOffset)
} else if (opts.preserveTimezones && timezoneOffset === undefined) {
date = LiquidDate.createDateFixedToTimezone(v, locale)
} else {
date = new LiquidDate(v, locale, defaultTimezoneOffset)
}
} else {
date = new LiquidDate(v, locale, defaultTimezoneOffset)
}
return date.valid() ? date : undefined
}

However, with some more checking I am seeing a test already where it seems this is the expected behavior. I don't understand why, but if it is correct then I'll have to work around it.

it('should treat nil as 0', () => {
expect(liquid.parseAndRenderSync('{{ nil | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}')).toEqual('1970-01-01T08:00:00')
})

Activity

  1. timbze commented on Apr 6, 2026

    @timbze
    ContributorAuthor

    I'd be happy to create PR if this logic should be changed.

  2. harttle commented on Apr 7, 2026

    @harttle
    Owner

    Thanks for the investigation. I checked shopify/liquid, it seems returning empty for this case. Let's align with the ruby version and change the test case above to equal ''.

  3. github-actions commented on Apr 7, 2026

    @github-actions

    🎉 This issue has been resolved in version 10.25.5 🎉

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions