Daylight Savings Time wrongly identified by Node.js

青春壹個敷衍的年華 提交于 2019-12-11 09:47:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25753537/daylight-savings-time-wrongly-identified-by-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!