Date.now() (in node.js) is targeting the universal time in ubuntu - node.js

I'm using ubuntu for serving my website and I've used Date.now() a couple of times to get the current date. I live in Beirut (which is around 12:40 PM right now) and Date.now() is logging minus 2 hours (10:40 PM).
I changed the localtime of ubuntu by doing this command:
sudo dpkg-reconfigure tzdata
It's printing this:
Local time is now: Tue Feb 27 12:40:59 EET 2018.
Universal Time is now: Tue Feb 27 10:40:59 UTC 2018.
But it still doesn't work.
Basically, I want Date.now() to target the local time instead of universal one...

Date.now() returns the milliseconds elapsed since the UNIX epoch, and has no concept of timezone whatsoever.

Related

Node.js Date does not consider DST (daylight saving time)

I'm living in Vancouver, west coast of North America, Canada. The original timezone here is PST (pacific standard time, UTC - 8h), but it's now PDT (pacific daylight time, UTC - 7h) since the daylight saving time has started.
The problem is that Node.js uses the original non-DST time, while Ubuntu returns DST.
In the following, both should be 21:44.
$ echo `date` , `node -e "console.log(new Date().toString())"`
Mon May 25 21:44:57 DST 2020 , Mon May 25 2020 20:44:57 GMT-0800 (GMT-08:00)
Why does this happen? How to let Node.js recognize DST?
I use Noce.js v12.16.3 on Ubuntu 18.04.4 as WSL on Windows 10. (Node.js v12.16.3 for Windows works correctly.) TZ env is not set.
UPDATE I don't want to install external libraries.
A more accurate library would be moment.js. You can set the time to recognize DST like this:
moment(timestamp).tz(timezone).isDST()
Here is an example:
moment(1451624400000).tz('America/New_York').isDST();
You can read more about recognized timestamps and timezones in the documentation on the website that I linked above.
My test shows that node does honor daylight saving time:
$ date; node -e "console.log(new Date().toString())";
Fri Jul 17 23:13:32 EDT 2020
Fri Jul 17 2020 23:13:32 GMT-0400 (Eastern Daylight Time)
$ date --date='Fri Jul 17 2020 23:13:32 GMT-0400'
Fri Jul 17 23:13:32 EDT 2020
$ node -v
v10.21.0
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal

AWS Linux Date vs node js Date

I have an AWS Instance running node v10.15.1
From linux:-
$ date
Thu 14 May 16:12:40 BST 2020
BUT node shows...
$ node
> new Date()
2020-05-14T15:13:37.941Z
Now the actual time is 16:12, So Node is showing GMT, but the System knows it really is BST.
My /etc/sysconfig/clock contains:-
ZONE="Europe/London"
UTC=true
It's possible that a re-start might sort this, but I'm trying to find an alternative, less intrusive solution. - any advice appreciated.
Date() in node always returns GMT time regardless of system time zone, you can convert to locale time :
[ec2-user ~]$ date
Thu May 14 17:38:13 BST 2020
[ec2-user ~]$ node
Welcome to Node.js v14.2.0.
Type ".help" for more information.
> d = new Date()
2020-05-14T16:38:54.746Z
> d.toLocaleTimeString()
'5:38:54 PM'

Servers with same timezone but different time

I have 3 servers, 2 on AWS and one on Digital Ocean, and the timezone for all is set to CDT. But when I check the current time on all 3 by using the date command via command line, none of them matches.
Server1: Wed Jun 12 23:36:01 CDT 2019
Server2: Wed Jun 12 23:45:51 CDT 2019
Server3: Wed Jun 12 23:38:39 CDT 2019
Could anyone please suggest what needs to be done here? Thanks.
Since you have not explicitly said that you have ntp running on them, you'll need to install that. Once that is installed and set up properly, you should show the same exact time on all of them.

Setting linux time properly (UTC)

I have problems with the time settings on my linux server. The timezone is set to UTC, the locale settings are the following: (locale -a)
C
C.UTF-8
en_GB.utf8
POSIX
date command gives me
Wed Jul 26 09:39:42 UTC 2017
but the correct time is 10:39
On my desktop I have the same locale settings and utc timezone and it gives me the correct time.
Could someone explain what's wrong with the server and what should be changed, please?
Thanks in advance.

Change system date time in Docker containers without impacting host

Is it possible to change the system time in Docker containers without changing the host itself?
We are using Docker containers for testing which are sometimes time sensitive. I notice that any changes made to the host time are reflected to the Docker containers right away (which makes sense since they are using the same kernel). I am hoping, however, that Docker provides a way for you to somehow override the start time of the container and move from thereon.
Just a follow up for those looking to have different datetime in Docker containers, https://github.com/wolfcw/libfaketime should probably be able to do the job.
Simple:
Steps:
First check the localtime of the docker container:
[root#locdb oracle]# zdump /etc/localtime
/etc/localtime Tue May 22 13:59:40 2018 UTC
Check the timezones like below:
[root#locdb oracle]# zdump /usr/share/zoneinfo/* | tail -10
/usr/share/zoneinfo/UTC Tue May 22 14:01:21 2018 UTC
/usr/share/zoneinfo/Universal Tue May 22 14:01:21 2018 UTC
/usr/share/zoneinfo/W-SU Tue May 22 17:01:21 2018 MSK
/usr/share/zoneinfo/WET Tue May 22 15:01:21 2018 WEST
/usr/share/zoneinfo/Zulu Tue May 22 14:01:21 2018 UTC
/usr/share/zoneinfo/iso3166.tab Tue May 22 14:01:21 2018
/usr/share/zoneinfo/posix Tue May 22 14:01:21 2018
/usr/share/zoneinfo/posixrules Tue May 22 10:01:21 2018 EDT
/usr/share/zoneinfo/right Tue May 22 14:01:21 2018
/usr/share/zoneinfo/zone.tab Tue May 22 14:01:21 2018
Copy the one that you need for example :
cp /usr/share/zoneinfo/UTC /etc/localtime
As described in detail here the solution that I follow is to run docker on top of a VM and automate the whole process of changing-vm-time, running-vm and running-container, via vagrant.

Resources