node.js time module timezone list - javascript

I am trying to googling for passing in acceptable parameters for setting up cronJobs based on a users timeZone. Im using a cronJob module which can be found here.
https://github.com/ncb000gt/node-cron
The problem is I need to pass different timeZones but I can't seem to find the acceptable parameters and format for the different timezones. Can anyone point me in the right direction?

Try converting the time to a common timezone before registering it with node-cron.
Here's a conversion example from another question.
From How to use timezone offset in Nodejs? :
var time = require('time');
var a = new time.Date(1337324400000);
a.setTimezone('Europe/Amsterdam');
console.log(a.toString()); // Fri May 18 2012 09:00:00 GMT+0200 (CEST)
a.setTimezone('Europe/Kiev');
console.log(a.toString()); // Fri May 18 2012 10:00:00 GMT+0300 (EEST)

I found it. For anyone encountering a similar problem here is a reference list by time zone acceptable for node.js. I believe its standard OS time zone stuff.
http://home.tiscali.nl/~t876506/TZworld.html

Related

getDate() giving wrong date due to Daylight saving timezone

I am struggling in formatting js date in a proper format and not sure if I'm doing this right.
So here's my case:
I import data strongly dependent on hours included in objects. Sometimes I received date with GMT +1 format and sometimes with GMT +2. I have figured out it depends on daylight saving date format.
But the problem is now, when I try to work on this data and I look for object from december, function getDate() would return me the day before (as hours would be 23:00 GMT +1 NOT 00:00 GMT +2)
I am working on client's oracle database and use it in nodejs server supported by oracledb npm package.
I was wondering if there is any nice / smooth way of unifying these date format to the same one? Like to receive all dates only in one of GMT +1/+2 format?
Thanks for any advice
What you'd like to do here is adjust the time according to getTimezoneOffset() and then store it.
MDN
The time-zone offset is the difference, in minutes, from local time to
UTC.
var d = new Date();
console.log(d)//Time before adjusting the timezone
d.setMinutes(d.getTimezoneOffset());
console.log(d) //time adjusted to UTC

Why there is a difference in time between node.js server and mongodb?

In my mongoDB database I have a document which attribute 'dia' is a Date filled with:
'ISODate("2018-09-07T20:00:00.000Z")'
The problem came when I try to get this document in my node.js server. Currently I'm using mongoose and when I do a model.findById('5b9d04c728c1640c07bb054f') it returns the object fine, except for the fact that the attribute 'dia' has the following value:
'Fri Sep 07 2018 17:00:00 GMT-0300 (-03)'.
Does anyone know why there is a difference between the date saved in my database and the one made by mongoose when I try to get the document?
Very likely it is saved as GMT and read in GMT-3 timezone. It may well be that node.js assumes that it gets GMT datetime and formats it according to your locale. You may want to either read the date with desired locale (instead of the default GMT) or print it in the locale GMT which should fix the difference.
The problem is that my database is storing every document in GMT (Greenwich Mean Time) and my server is running in my local timezone which is GMT(-3). To use the correct date for querying mongoDB, I used moment.js with the following parameters:
var date = moment.utc(dia).format();
getting as result 'date = 2018-09-01T20:00:00Z', which is the same time as in the database.
UTC (Coordinated Universal Time) is the same as GMT, but it is being using nowadays.

Weird Javascript Timezones with Date()

Javascript hopeless here but I've got a problem I can't seem to understand, and don't know whether it's defined behavior in JS or not...
From what I can understand, new Date() returns the current time in the current timezone as reported by the browser. At the current time of writing this, I am in Milan, Italy (GMT +0200 CEST), so when performing the below:
var date = new Date();
console.log(date);
console.log(date.getTimezoneOffset());
I get, as expected:
Sun Oct 07 2018 15:42:12 GMT+0200 (Central European Summer Time)
-120
However, again from what I understand, throwing milliseconds into the Date Constructor creates a date based off of Epoch Time, 1st January 1970 00:00:00 GMT +0000 - So why if I perform:
var date = new Date(0); // Note The 0
console.log(date);
console.log(date.getTimezoneOffset())
Do I get:
Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
-60
Did the browser for some reason change the timezone it thinks I am in? This is causing quite a few problems for me retrieving a unix timestamp from a database, and displaying it in the user's current timezone!
Any help or advice on this would be much appreciated!
Many thanks! :)
The numeric timestamp value inside every Date instance, no matter how it is constructed, represents an offset from a fixed UTC "epoch" time. When you construct a date in your locale, the timezone offset is taken into account when the Date instance timstamp is set. The timezone offset is also taken into account when you access parts of the date via locale-relative APIs like .getTimezoneOffset().
Thus what matters is how you get date/time information out of the Date instance. If you use UTC APIs, you get UTC time information. If you use locale-relative APIs, you get locale-relative values.
let d = new Date();
console.log("locale-relative: " + d);
console.log("UTC: " + d.toUTCString());
Now, as to how you handle the system timestamp values you've got in your database, that depends on how your application works and what the dates mean in that context. If it's important that users see the dates in terms of what your server(s) will do with them, format the dates on the server with locale-relative APIs. That would make sense if your application does some work based on local time. For example some banking applications (in the US) do things at night, but "night" in terms of late evening hours in the continental US.
If, on the other hand, the dates you store should be shown to your users in terms of their locales, then send the client the timestamp and format your dates as strings at the client with locale-relative APIs. That would be appropriate for an application that allows users to set up alarms etc: generally it makes sense for the user to think in terms of their own local time.

Javascript Date Gives me EDT but I want EST

I am receiving a MySQL Timestamp in UTC and trying to covert it to the client's local timezone. However, When i do this I get the wrong time zone.
Ive formatted my DateTime String to be: var utcTime = 2014-05-15T13:00:00Z
However when after my conversion my dateObject is: Date {Thu May 15 2014 09:00:00 GMT-0400 (EDT)}. However, I want my Timezone to be GMT -0500 (EST).
I've searched online and saw there is a way to do this by appending "UTC" to a MYSQL formatted Timestamp.. However, this method does not work in all browsers.
If anyone has any insight on converting timezones i would appreciate it.
The D in EDT stands for Daylight and the S in EST stands for Standard. EDT should be used during Summer in the U.S. and EST in the Winter (list of countries here). Is it possible that GMT -4 (EDT) is actually the right local time? If it would be more towards winter it would switch automatically to GMT -5 (EST). The client timezone together with daylight savings is handled automatically by Javascript.
For example, the default string representation of a certain date in Javascript should correctly choose between Standard time and Daylight Savings time based on the date object itself and the machine timezone:
var date = new Date(millisSinceUnixEpoch);
alert(date.toDateString() + ' ' + date.toTimeString());
Note: there's room for a lot of assumption though. E.g. not sure exactly how your 'conversion to local timezone' code looks like
I've seen something similar to this. This MSDN article may explain it.
Handling daylight saving time using JavaScript
http://msdn.microsoft.com/en-us/library/ie/jj863688%28v=vs.85%29.aspx
In Windows Internet Explorer 9 and previous versions of Windows
Internet Explorer, dates are customized by applying the ECMAScript
specification's rules for storing daylight saving time adjusted times
internally. To improve accuracy, especially with dates in the past
(historical dates), Internet Explorer 10 relies on the system's rules
for storing daylight saving time adjusted times. This topic contains
the following sections:

Daylight Savings Time wrongly identified by Node.js

I'm having an issue with timezones, DST and Node.js. It doesn't identify correctly DST effects. My OS is Windows 8.1 and have the timezone value and DST well set. I tried these two methods:
1) Node's Date Object
I typed the following code in Node Prompt and received the answer in italic:
new Date()
Tue Sep 09 2014 18:42:36 GMT-0200 (Horário brasileiro de verão(Brazilian Daylight Savings Time))
However, the brazilian DST starts just in Oct 19th, we are not in DST right now. Therefore, the hour is shifted 1 hour from now.
2) node-time
I got node-time package from npm, to be able to change Timezone. In the code:
time = require('time');
dateFormat = require('dateformat');
now = time.Date();
console.log(dateFormat(now.setTimezone('America/Sao_Paulo').getTime(), "yyyy-mm-dd HH:MM:ss Z"))
and as a result, I got
2014-09-09 18:42:36 GMT-0200
Brazilian normal timezone is GMT-0300. Hour is also 1 hour shifted.
Am I doing something wrong? Is there a workaround?
The node-time package does not work on Windows. I couldn't even get it installed, so I'm not sure how you did. You should uninstall it.
If you need support for non-local time zones in Node (or in the browser), I recommend moment.js with the moment-timezone plugin.
Running new Date() on Windows with the time zone set for Brazil, I get the correct output.
The only thing that would explain your results is if the time zone registry settings were modified - but then you'd also see the wrong time in your system clock on your taskbar. So I'm sorry, but I cannot reproduce that part of it.

Categories