Logo

dev-resources.site

for different kinds of informations.

Solve UK time changes (DST) with NodeJS and date-fns and Docker (epoch/unix format)

Published at
6/21/2022
Categories
javascript
docker
timezone
node
Author
zainzzkk
Categories
4 categories in total
javascript
open
docker
open
timezone
open
node
open
Author
8 person written this
zainzzkk
open
Solve UK time changes (DST) with NodeJS and date-fns and Docker (epoch/unix format)

DST time changes can wreck havoc when in the UK time-zone, where you'll find your times might be one hour behind, especially when using epoch/unix format to save your timestamps.

When using NodeJS with date-fns in a docker container, this simple guide will show how to fix the issue.

new Date(); might return a time in GMT as compared to DST. Furthermore, using getUnixTime() from date-fns will return an epoch timestamp which will be in GMT. Converting this back to BST may present a challenge.

const { getUnixTime, format } = require("date-fns");

const date = new Date();
console.log('new Date() print',date, '\n');

const unixTime = getUnixTime(date);

const formatted = format(new Date(unixTime * 1000), 'h:mm aa', {
    timeZone: 'Europe/London',
});
console.log('formatted timestamp with timezone', formatted);

Enter fullscreen mode Exit fullscreen mode

Running the above code will give you a timestamp which takes DST into account for correct BST as shown by this screenshot from terminal.

Image description

You may be wondering about the unixTime * 1000. This is because date-fns getUnixTime() gives a unix timestamp without milliseconds, and hence converts to seconds.

The date-fns format() function accepts a third parameter for time-zone which is why we have used { timeZone: 'Europe/London', }.

If running in a docker container and it still returns a GMT timestamp, then adding - TZ=Europe/London as part of the environment section of your docker-compose file will help solve the issue by setting the container time-zone to London. Example below:

test:
    build: ./test
    image: ....
    depends_on:
      - ....
    ports:
      - "1234:1234" # http
      - "1234:1234" # debug
    volumes:
      - ....
      - ....
      - ....
    environment:
      - PORT=....
      - TZ=Europe/London
    command: ....
Enter fullscreen mode Exit fullscreen mode
timezone Article's
30 articles in total
Favicon
Converting date by user time zone in "NestJS", and entering and displaying date in "Angular"
Favicon
Timezone support in a full-stack application based on NestJS and Angular: working with REST and WebSockets
Favicon
Python: it is now() time to migrate from utcnow()
Favicon
geo2tz - 4 years later
Favicon
🌐 How to Change Time Zone in Google Chrome to Test Different Timezones
Favicon
¿Cómo trabajar correctamente con fechas?
Favicon
How to fix Dynamics AX 2012 R3 when the time is 1 hour ahead when not using daylight savings time
Favicon
Config Timezone Laravel in Your Project
Favicon
Javascript Zoned-Date library - fully DST support
Favicon
ChronoMate: Your Ultimate Time Zone Companion for Seamless Productivity and Scheduling Ease!
Favicon
supabase timezone + cron
Favicon
Command Prompt - Set Timezone
Favicon
Troubleshooting Timezone Issues in PostgreSQL with DBeaver
Favicon
Fixed: WHM (CPanel) + Laravel Timezone Issue
Favicon
Solve UK time changes (DST) with NodeJS and date-fns and Docker (epoch/unix format)
Favicon
Is there any reason to use ZoneId.of("UTC") instead of ZoneOffset.UTC?
Favicon
Debugging timezone issue in Java [Linux]
Favicon
Local time of employees in Notion
Favicon
Modificando a hora nas configurações do PHP8
Favicon
Time change in daylight saving
Favicon
Avoid this when using Date/Time functions in PHP
Favicon
Timezone for DateTime Field at Laravel Nova
Favicon
NTP (Network Time Protocol) Setup for Linux (Ubuntu) Server
Favicon
[Phoenix LiveView] formatting date/time with local time zone
Favicon
Shift the TZ for your K8s CronJobs
Favicon
Datetimes Are Hard: Part 2 - Writing and running code
Favicon
Where does GMT-0456 Timezone Come From?
Favicon
Dealing with Timezone in JavaScript
Favicon
Date and time gotchas
Favicon
How to avoid and debug most of timezone problems in production

Featured ones: