Server UTC time and date.js getTimezoneOffset different

138 Views Asked by At

I have my code hosted on my local and on a server, and milliseconds since Epoch is behaving unexpectedly on my server.

I have new Date("07/16/2018 10:17:48").getTime() hardcoded, and on my local, result is 1531761468000, server: 1531736268000. Server is 7 hours behind.

I ran date +%z, local: -0700, server: -0700

I ran date.getTimezoneOffset(), local: 420 (converts to UTC-7), server: 0 (converts to UTC)

What may be the reason for this discrepancy? I have no idea how to fix it.

1

There are 1 best solutions below

2
Sebastian Freitag On

I assume you run Node.js on the server. Please compare process.env for server and local.

console.log(process.env);

Consider process.env.TZ! Your node process will not use a call of date(1) but instead honor the environment. If this is indeed the reason, you could fix it by adjusting the actual environment (via supervisor or whatever you use to start your process) or by changing it just after start inside your script.

This post should be helpful by showing you some options you have to control the environment of your server process: https://medium.freecodecamp.org/heres-how-you-can-actually-use-node-environment-variables-8fdf98f53a0a