Frontend Development 5 min read

Why Did My JavaScript Timestamp Show the Wrong Date? A Dayjs Debugging Guide

A developer discovers a bug where a time component displays incorrect dates because the backend switched timestamp values from numbers to strings, and learns how to correctly parse timestamps in JavaScript using Dayjs and defensive Number conversion.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Why Did My JavaScript Timestamp Show the Wrong Date? A Dayjs Debugging Guide

Introduction

A few days ago a tester reported a bug: the time component displayed an abnormal date. The data was initialized from the backend, so I asked the backend whether the timestamp was wrong. They said the format had always been a numeric timestamp and never caused issues.

Comparing production data revealed the root cause: the backend changed the timestamp type from Number to String .

Bug Cause

The issue lies in how the timestamp is parsed. In the code I consistently use dayjs for date parsing.

Typical parsing code:

<code>const time = dayjs(res.endTime).format('YYYY-MM-DD HH:mm:ss')</code>

I tried parsing both string and numeric forms:

String: dayjs('175008959900').format('YYYY-MM-DD hh:mm:ss') // 1975-07-19 01:35:59

Number: dayjs(Number('175008959900')).format('YYYY-MM-DD HH:mm:ss') // 2025-07-17 06:59:59

Because the backend now returns a string like '175008959900' , dayjs() treats it as a generic date string and does not automatically recognize it as a millisecond timestamp, leading to invalid parsing or fallback to the Unix epoch.

How to Avoid This Issue

Just like new Date() , both Date and dayjs should always receive a numeric timestamp. Convert the input defensively:

<code>const ts = Number(res.endTime);
const date = new Date(ts);
</code>

Reflection

A timestamp is a numeric representation of time, usually counting the elapsed milliseconds (or seconds) since the Unix epoch (1970‑01‑01 00:00:00 UTC).

Common timestamp types:

Unix timestamp (seconds) – e.g., 1750089599 – typical in backend APIs and databases.

Millisecond timestamp – e.g., 1750089599000 – common in JavaScript (e.g., Date.now() ).

Significance of timestamps:

Provides an absolute numeric representation of time that is language‑ and platform‑agnostic.

Enables easy arithmetic: subtracting two timestamps yields the interval in milliseconds.

More compact and performant than formatted date strings.

Using timestamps in JavaScript:

<code>console.log(Date.now()); // e.g., 1714729530000
console.log(new Date(1750089599000)); // Thu Jul 17 2025 06:59:59 GMT+0800
</code>
frontendJavaScripttimestampdayjsDate Parsing
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.