Nodejs stores Date value one hour earlier in mongodb - node.js

I’m trying to save a Date value from nodejs in my mongodb, but the saved value is one day earlier. More exactly its one hour.
If I use console.log(new Date(this.year, this.month, this.day)); in nodejs I get:
Tue Mar 19 2013 00:00:00 GMT+0100 (CET)
but in mongodb I have following record:
2013-03-18 23:00:00 +0000
the record is one hour earlier.
But why? Have I to set the timezone in mongodb? I'm using Os X with:
Time Zone: Europe/Berlin

MongoDB stores time in UTC only. It is left to the application to do timezone conversions.

It's not one hour earlier — it's stored in mongodb using timezone +00:00 instead of +01:00.
00:00 by +01:00 === 23:00 by +00:00

To avoid the timezone problem, just add process.env.TZ = 'UTC' in the "main".

Related

the time that now() function returns is different from system time

I get result as follow when I execute a sql which includes now()function in VoltDB
select * from test2;
C1
---------------------------
2019-06-29 07:13:38.050000
But,I get another time when I execute a shell command 'date'
[root#localhost config]# date
Sat Jun 29 03:30:09 EDT 2019
How can I make them same?
VoltDB stores time in the UTC timezone. If you want to change this to EDT or any other timezone, you should be able to do it using a variety of methods and languages.
For example, in java you might use VoltTableRow.getTimestampAsSqlTimestamp(0) to get the value of a timestamp row. You could then use DateFormat to convert this to a particular timezone.
You can read more about the "getTimestampAsSqlTimestamp" method here: https://docs.voltdb.com/javadoc/java-client-api/org/voltdb/VoltTableRow.html
Full Disclosure: I work at VoltDB.

Node.js new Date does not reflect timezone TZ var from .env

just stumbled upon one funny issue in Node.js & MacOs - basically if you run instance of node with set timezone var in .env file, variable TZ, and you local machine is in different timezone, the Date creation is f**d up in the wort way possible, so that:
Timezone is set correctly
But when you create a new Date, the timezone is not reflected
see this example, (my machine is in CET, timezone Europe/Prague) :
~/
11:13 (CET) $ node
new Date()
2019-02-04T10:30:23.053Z # Europe/Prague
new Date().getTimezoneOffset()
-60 # Europe/Prague
.exit
vs. set timezone in TZ env
✔ ~/
11:30 (CET) $ TZ=America/New_York node
new Date()
2019-02-04T10:31:40.968Z # 10:31! :D
new Date().getTimezoneOffset()
300 # America/New_York!
So it is not possible to create a correct date then, since all JS libs like moment uses in the end Date object.
My take is that you should never ever set TZ var while running Node.js server, do I miss something?
Your examples look correct to me. I suspect that your notion of the way the computer keeps track of, and shows, time is upside-down. Your comments imply that you think TZ is used to derive UTC from the computer's internal clock. That's not how it works.
Internally, your Mac is using UTC time. Your Mac is not "in CET". It is "in UTC with a default TZ setting of CET". TZ controls how a local time is derived from the underlying UTC when a local timestamp or timezone offset is required. If you change TZ then local timestamps will be shown in the new timezone, but the computer's internal timekeeping in UTC is not affected.
Specifically for your example, new Date() gets you a Date object representing the current time. The default string representation of a Date object is the UTC time. In your example you can see that this time, shown as the result of the new Date() call:
2019-02-04T10:30:23.053Z # Europe/Prague
is a UTC time because its timezone is shown as Z, indicating UTC. (One way to remember that meaning is that "Z" is for "Zero offset from UTC". This format is sometimes called "Zulu" time because "Zulu" is the ICAO phonetic alphabet's code word for "Z".) If your comment is claiming that this is a local Europe/Prague timestamp then the comment is incorrect.
The result of the second new Date():
2019-02-04T10:31:40.968Z # 10:31! :D
is also shown in UTC and is about a minute later than the first result. It doesn't matter that TZ for this process is different, because TZ does not affect UTC timestamps.
To see the local time, which is calculated by applying the TZ setting to the UTC time obtained from the computer's clock, use the toLocaleString method on the Date object. If you repeat your test using that method you'll see something like this:
$ env TZ=Europe/Prague node
> now = new Date()
2019-02-04T20:26:40.408Z
> now.toLocaleString()
'Mon Feb 04 2019 21:26:40 GMT+0100 (CET)'
$ env TZ=America/New_York node
> now = new Date()
2019-02-04T20:27:12.438Z
> now.toLocaleString()
'Mon Feb 04 2019 15:27:12 GMT-0500 (EST)'
which looks perfectly reasonable.
BTW, Node, or JavaScript, isn't doing anything unusual here. This is the way it works for everything. For example, the date command:
$ env TZ=Europe/Prague date
Mon Feb 4 21:54:49 CET 2019
$ env TZ=America/New_York date
Mon Feb 4 15:54:51 EST 2019
You can solve your issue with moment-timezone node package. below code may help you to resolve your issue.
const moment = require('moment-timezone')
function convertDate(dateInUTCFormat) {
return new Promise((resolve, reject) => {
var dec = moment(dateInUTCFormat);
var normalDate = dec.tz('Asia/Kolkata').format('YYYY-MM-DD'); // GMT +5:30 (India)
resolve(normalDate);
})
}
Also you need to pass the date format that you get in UTC format for eg 2019-02-04T10:30:23.053Z to convertDate function and pass your timezone as i passed my timezone Asia/Kolkata . May this will help you to find your solution.

how to get local date/time in linux terminal while server configured in UTC/different timezone?

how to get local date & time in linux terminal while server configured in UTC or different timezone?
here is what I get now but I'd like to see in local timezone. For eg: PST/PDT.
[jenkins#myServer ~]$ date
Thu Jul 28 18:16:48 UTC 2016
I'm not looking to change system time using hwclock or updating /etc/localtime. Just want to change it for a user.
Also, please let me know - how to persist it for future logins.
Use the TZ environment variable to pass the desired timezone to date:
TZ=<timezone> date
You can find the available timezones in the /usr/share/zoneinfo/ directory and subdirectories. For example, /usr/share/zoneinfo/America/New_York defines TZ=America/New_York.
Example:
$ date
Fri Jul 29 06:31:53 BDT 2016
$ TZ='America/New_York' date
Thu Jul 28 20:31:58 EDT 2016
$ TZ='America/Los_Angeles' date
Thu Jul 28 17:31:54 PDT 2016
For a local time, use "date". For UTC time, use "date -u". Note that, if you use "date" in the server's terminal it returns the server local time.
You can show local time by overriding the TZ environment variable for the process which prints the date. POSIX says a lot about the topic, beginning with
This variable shall represent timezone information. The contents of the environment variable named TZ shall be used by the ctime(), ctime_r(), localtime(), localtime_r() strftime(), mktime(), functions, and by various utilities, to override the default timezone.
Conventional 3-character timezone values were some time ago (more or less) standardized to deprecate the 3-character forms, using the combined standard and daylight savings time form. The preferred form used for PDT is PST8PDT.
There's a page on VMware showing the names and mentioning that they are used on Linux; you may notice that very few of those are 3-character form (aside from the generic UTC+offset).
Further reading:
Valid timeZone Values (VMware):
IANA - Time Zone Database

browser shows wrong DST when setting date

I need the browser to display a time (in milliseconds) sent from the server, in the user's local timezone & dst.
I noticed that when I ask the browser to show the current time (now) then the Timezone and DST are right. However, when I set a date and ask the browser to show the set time then the Timezone is right but the DST is wrong.
Here is actual code:
x = new Date(1357012800*1000);
console.log("Javascript mydate:"+x);
now = new Date();
console.log("Javsscript now:"+now);
And the output from Chrome:
Javascript mydate:Mon Dec 31 2012 23:00:00 GMT-0500 (Eastern Standard Time)
Javsscript now:Fri Apr 05 2013 11:04:41 GMT-0400 (Eastern Daylight Time)
Can someone tell me how to make the browser display the date respecting the user's DST? Why is it printing mydate in EST yet it's now time in EDT?
The date you provided, Dec 31 2012, Eastern Time was not in daylight savings.

Timezone in date?

From where does the date command in Linux get the timezone information?
I cannot see /etc/localtime file and /usr/share/zoneinfo directory in my system. Still when i execute the date command i get the following output
Thu Dec 9 16:28:18 UTC 2010
Kindly tell me from where does the command get the timezone information?
Thanks,
LinuxPenseur
Don't forget that UTC is how standard Unix systems store the date/time in the real time clock. You have to jump through hoops using funny programs (see the hwclock(8) manpage) if you dual-boot to Windows, which prefers the local time to be stored in the CMOS real time clock.
So the date(1) program is simply showing you the results of "I have no configured time zone":
# date -u
Thu Dec 9 10:40:54 UTC 2010
# TZ=UTC date
Thu Dec 9 10:40:57 UTC 2010
# TZ=PST8PDT date
Thu Dec 9 02:41:02 PST 2010
#
From http://www.wikihow.com/Change-the-Timezone-in-Linux
On mobile phones and other small devices that run Linux, the time zone
is stored differently. It is written in /etc/TZ, in the format that is
described, for instance, in [4] . Edit this file manually or use
echo (for instance, echo GMT0BST > /etc/TZ to set the the timezone of
the United Kingdom).
From http://www.radisys.com/files/support_downloads/03245-02_MPCMM0001_MPCMM0002_CMM_Software_TPS.pdf
The CMM determines the offset to local timezone maintained in file
/etc/cmm/TZ and automatically updates the time.
This should help :
http://www.hypexr.org/linux_date_time_help.php

Resources