Dayjs timezone causes Invalid Date

Hi Using dayjs, I want to change the timezone from UTC to the user timezone, e.g. Europe/Copenhagen. Using the demo example from dayjs: dayjs("2013-11-18 11:55:20").tz("America/Toronto") Produces: "Invalid Date" https://day.js.org/docs/en/timezone/timezone Node version 16.18.1
Time Zone · Day.js
Day.js supports time zone via the Internationalization API in supported environments. By using the native API, no extra bytes of tim...
14 Replies
Percy
Percy2y ago
Project ID: 860a5b73-658e-40e5-9897-6aac8389fe03
×huggi×
×huggi×OP2y ago
860a5b73-658e-40e5-9897-6aac8389fe03
Brody
Brody2y ago
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(timezone);

const time = dayjs.utc('2013-11-18 11:55:20').tz('America/Toronto');

console.log(time.format());
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(timezone);

const time = dayjs.utc('2013-11-18 11:55:20').tz('America/Toronto');

console.log(time.format());
outputs
2013-11-18T06:55:20-05:00
2013-11-18T06:55:20-05:00
though I am on Node.js v18.4.0
×huggi×
×huggi×OP2y ago
Thank you for taking your time. Did you run that snippet on railway? Because in most other node environments it works for me, just not on railway. Your snippet works for example fine in 16.18.1 on runkit: https://npm.runkit.com/dayjs
RunKit
RunKit notebooks are interactive javascript playgrounds connected to a complete node environment right in your browser. Every npm module pre-installed.
Brody
Brody2y ago
I have not, ran locally, I will try to run that code on railway and get back to you okay after some testing, I think what's happening is there is no zoneinfo being built into the container, or for whatever reason, it's not being detected by node. the easiest solution I can think of would be to build with a Dockerfile like this:
FROM node:18.14.0-alpine3.17

WORKDIR /app

ADD . /app

RUN npm install

CMD ["npm", "start"]
FROM node:18.14.0-alpine3.17

WORKDIR /app

ADD . /app

RUN npm install

CMD ["npm", "start"]
the above code works as expected on railway when built with that Dockerfile
angelo
angelo2y ago
I think we do need to document this. Lemme see what we can do here. Nothing immediate can be done unless we look at the builder + provider.
×huggi×
×huggi×OP2y ago
Sorry for my late reply. I don't get Discord notifications for some reason. If i run: dayjs.tz.guess() i get "UTC". Not sure if that's just default or related to zoneinfo. Is it possible to add zoneinfo manually? I'm deploying Strapi on railway. Is there a specific dockerfile for Strapi? I tried to add environment variable TZ="America/Toronto" and that does change the timestamps in the logs and the dayjs guess, but still get Invalid Date
×huggi×
×huggi×OP2y ago
Brody
Brody2y ago
are you deploying strapi from a template?
×huggi×
×huggi×OP2y ago
Yes, the strapi template github.com/railwayapp-templates/strapi
Brody
Brody2y ago
yeah that builds with nixpacks that doesn't seem to be including timezone data in the final container, you would need to clone that repo and add a dockerfile like the one above, or wait until this missing timezone thing gets figured out by a team member. but beyond that i cant help because ive never used strapi, sorry two other people have had issues related to missing timezone data, on python so i hope this gets looked at soon
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
×huggi×
×huggi×OP2y ago
Thank you for linking that issue. I can confirm that patching dayjs makes timezone work. Follow the link for details on this issue.
imIgnacio
imIgnacio2y ago
@×huggi× hi man, how did you patch it?
Want results from more Discord servers?
Add your server