Skip to content

Date changed representation when using utils.format() with "%s" #57818

Closed
@robross0606

Description

@robross0606

Version

v20.15.0

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

No response

What steps will reproduce the bug?

Using Node 20.14.0 or earlier:

import { format, inspect } from 'node:util'

const myDate = new Date()

const formattedString = format('%s', myDate )
console.log(`formattedString = ${formattedString}`)

const inspectedString = inspect(myDate )
console.log(`inspectedString = ${inspectedString}`)

Outputs:

formattedString = 2025-04-10T12:38:19.165Z
inspectedString = 2025-04-10T12:38:19.165Z

Using Node 20.15.0 or later:

formattedString = Thu Apr 10 2025 08:38:49 GMT-0400 (Eastern Daylight Time)
inspectedString = 2025-04-10T12:38:49.558Z

How often does it reproduce? Is there a required condition?

Using Node v20.15.0 or later.

What is the expected behavior? Why is that the expected behavior?

As per Node documentation for utils.format():

%s: String will be used to convert all values except BigInt, Object and -0. BigInt values will be represented with an n and Objects that have no user defined toString function are inspected using util.inspect() with options { depth: 0, colors: false, compact: 3 }.

This documented behavior could be confirmed by comparing util.inspect(new Date()) to util.format('%s', new Date()).

What do you see instead?

util.inspect(new Date()) now significantly deviates from util.format('%s', new Date()). This is both a somewhat breaking change and not reflected in the documentation.

Additional information

I believe this was broken by pull request #50992.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions