在nodejs程序中使用timezone-js [英] Using timezone-js in nodejs program
问题描述
以下是我编写的代码,使用 timezone-js
模块为特定时区创建 Date
对象
Here is the code that I wrote to use timezone-js
module to create Date
object for a specific timezone
require('timezone-js');
var dt = new timezoneJS.Date('2012, 06, 8, 11, 55, 4','Europe/Amsterdam');
我跑 npm install timezone-js
和安装模块。
然而,当我运行程序时,我收到以下错误
However , when I run the program, I get the following error
var dt = new timezoneJS.Date('2012, 06, 8, 11, 55, 4','Europe/Amsterdam');
^
ReferenceError: timezoneJS is not defined
at Object.<anonymous> (/Users/nandish/Documents/MI-Airlines/mi.airline-sync/lib/nodeTest.js:47:15)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
推荐答案
当您使用 require
时,您将结果分配给变量,然后使用该变量。所以:
When you use require
, you assign the result to a variable, and then use that variable. So:
var timezoneJS = require('timezone-js');
除此之外,这意味着你可以使用你想要的任何名称,例如:
Amongst other things, this means that you can use whatever name you want, for instance:
var ts = require('timezone-js');
var dt = new ts.Date(2013, 0, 1);
附注:请务必阅读 timezone-js
的设置要求你打算用它。它需要区域文件,如果您不提供它们,它将尝试动态加载。我试过的版本有错误检测可用的传输,FWIW(它试图读取它不知道定义的全局符号,这会引发异常,而不是使用 typeof thesymbol
哪个没有)。
Side note: Be sure to read about the setup requirements for timezone-js
if you're going to use it. It needs zone files, which it will try to load dynamically if you don't supply them. The version I tried out has bugs detecting what transport is available, FWIW (it tries to read a global symbol it doesn't know is defined, which throws an exception, rather than using typeof thesymbol
which doesn't).
如果您主动抓取该链接中描述的时区文件,并且它们位于相对路径 tz
从您运行脚本的位置开始,这可行:
If you proactive grab the timezone files as described in that link, and they're in the relative path tz
from where you're running your script, this works:
var timezoneJS = require("timezone-js");
var fs = require('fs');
// Give timezoneJS a transport for loading local timezone files.
// This code is extracted from https://github.com/mde/timezone-js/blob/master/spec/test-utils.js
timezoneJS.timezone.transport = function (opts) {
// No success handler, what's the point?
if (opts.async) {
if (typeof opts.success !== 'function') return;
opts.error = opts.error || console.error;
return fs.readFile(opts.url, 'utf8', function (err, data) {
return err ? opts.error(err) : opts.success(data);
});
}
return fs.readFileSync(opts.url, 'utf8');
};
// Init timezones
timezoneJS.timezone.zoneFileBasePath = 'tz'; // <== If you put the files elsewhere, change this
timezoneJS.timezone.init();
// Use it to get a date in Central European Time
var d = new timezoneJS.Date(2013, 7, 1, 'CET');
console.log(d.toString()); // "Local" time for the date instance (CET)
console.log(d.toUTCString()); // UTC
...当然,您可以使用使用HTTP加载它们的传输。我很惊讶 npm -
-packaged版本的 timezone-js
不会默认为某些基于文件的传输而不是寻找图书馆(破损检查)。
...or of course you can use a transport that uses HTTP to load them. I'm surprised the npm-
-packaged version of timezone-js
doesn't default to some file-based transport rather than looking for libraries (with the broken check).
这篇关于在nodejs程序中使用timezone-js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!