dayjs format() prints the previous or next day
A stored date of 2024-03-10 prints as the day before (or after) in the browser or on the serverShort answer
dayjs works in the local time zone by default. A UTC timestamp (or a date-only string, which JavaScript reads as UTC) is shifted into local time when formatted, which can cross midnight. Parse and format in UTC with the utc plugin, or convert to a named zone with the timezone plugin.
Fix it fast
1Find out which cause you have
Print the same instant in UTC and in local time, next to the server's offset. A different date between the two lines confirms a zone shift.
node -e "const d=new Date('2024-03-10'); console.log(d.toISOString(), d.toString())"2Match what you see, then jump to its fix
- 1
The date is off by one only on machines west of UTC (or east, for the opposite shift)A date-only or UTC value is formatted in the server's local zoneFix - 2
Dates are right in one country and off by one for users elsewhereYou need a specific zone, not UTC and not the server zoneFix - 3
Two servers parse the same string to different instantsThe input string has no offset, so it is read in the wrong zoneFix
- 1
What does the error look like?
A stored date of 2024-03-10 prints as the day before (or after) in the browser or on the server
Cause 1 of 3
A date-only or UTC value is formatted in the server's local zone
1Check
Without an offset, a date-only string such as 2024-03-10 is read as UTC, and a date-time string without an offset as local time. A database Date that holds UTC midnight then displays as the previous evening in a western zone.
node -e "console.log(new Date('2024-03-10').toISOString(), new Date('2024-03-10T00:00:00').toISOString())"2Fix
Use the utc plugin so the value is parsed and printed in UTC.
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
dayjs.extend(utc);
dayjs.utc('2024-03-10').format('YYYY-MM-DD'); // 2024-03-103Verify
The printed date matches the stored one on every machine.
TZ=America/New_York node -e "const dayjs=require('dayjs');dayjs.extend(require('dayjs/plugin/utc'));console.log(dayjs.utc('2024-03-10').format('YYYY-MM-DD'))"Confirmed by day.js.org
Cause 2 of 3
You need a specific zone, not UTC and not the server zone
1Check
dayjs(value) always uses the local zone, even after dayjs.tz.setDefault. Only dayjs.tz() reads the default zone.
node -e "console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)"2Fix
Load both plugins. Parse as UTC and convert to the zone you want to show.
const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/utc'));
dayjs.extend(require('dayjs/plugin/timezone'));
dayjs.utc('2024-03-10T22:00:00Z').tz('Asia/Taipei').format('YYYY-MM-DD HH:mm');3Verify
The result carries the expected local date.
console.log(dayjs.utc('2024-03-10T22:00:00Z').tz('Asia/Taipei').format())Confirmed by day.js.org
Cause 3 of 3
The input string has no offset, so it is read in the wrong zone
1Check
A string with Z or an offset such as +02:00 names an instant. A string without one depends on how it is read, so the result varies by environment.
node -e "const dayjs=require('dayjs'); console.log(dayjs('2018-04-13 19:18').toISOString())"2Fix
Store and send timestamps with Z or an explicit offset, or parse a zone-less string with dayjs.tz and the zone you mean.
dayjs.extend(require('dayjs/plugin/utc'));
dayjs.extend(require('dayjs/plugin/timezone'));
dayjs.tz('2018-04-13 19:18', 'Europe/Dublin');3Verify
Same string, same instant on every machine.
console.log(dayjs.tz('2018-04-13 19:18', 'Europe/Dublin').toISOString())Confirmed by day.js.org
How do I stop it coming back?
- Store timestamps in UTC and convert to a zone only when displaying.
- Send dates between services as ISO strings with Z or an offset.
- Run tests with TZ set to a far-off zone to expose hidden zone shifts.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix