Javascript 类型错误:moment().tz 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47046067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:42:46  来源:igfitidea点击:

TypeError: moment().tz is not a function

javascriptunit-testingjasminemomentjs

提问by Sanath

When testing using jasmine, I am getting this error.

使用 jasmine 进行测试时,出现此错误。

TypeError: moment.tz is not a function

类型错误:moment.tz 不是函数

My code that I try to test is

我尝试测试的代码是

let myDate = moment().tz(undefined, vm.timeZone).format('YYYY-MM-DD'); 

回答by Matt Goodrich

Fix

使固定

If you're using Node.js, you may accidentally be using

如果您使用的是 Node.js,您可能会不小心使用

const moment = require('moment');//moment

const moment = require('moment');//时刻

instead of

代替

const moment = require('moment-timezone');//moment-timezone

const moment = require('moment-timezone');//时刻-时区

Also, make sure you have installed moment-timezone with

另外,请确保您已经安装了moment-timezone

npm install moment-timezone --save

npm install moment-timezone --save

Explanation

解释

The bug of requiring moment without timezones could occur by installing moment with require('moment'), later deciding to npm install moment-timezone, and then forgetting to update the require.

通过安装 moment with require('moment'),稍后决定安装npm install moment-timezone,然后忘记更新require.

回答by Purushottam Sadh

Below code for me...

下面的代码给我...

import moment from 'moment';
import 'moment-timezone';

回答by Xu Tongbin

I've encountered this problem too. It works for years, but after a refactor, it doesn't work. As I've investigated, [email protected]depends on moment@>=2.9.0, which might be different from momentitself.

我也遇到过这个问题。它可以工作多年,但经过重构后,它不起作用。正如我所调查的,[email protected]取决于moment@>=2.9.0,这可能与moment它自己不同。

In my case, moment-timezoneuses [email protected], and momentitself version is 2.18.1. Causes moment-timezonedecorated wrongversion of moment.

在我的情况下,moment-timezone使用[email protected],moment本身版本是2.18.1. 原因moment-timezone装饰错误的版本moment

I've change yarn.lock like this:

我已经改变了 yarn.lock 这样的:

[email protected]:
  version "0.5.13"
  resolved "https://arti-dev.ss.aws.fwmrm.net/api/npm/fw-npm/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90"
  integrity sha1-mc5cfYJyYusPH3AgRBd/YHRde5A=
  dependencies:
    moment ">= 2.9.0"

[email protected], moment@>= 2.9.0:
  version "2.18.1"
  resolved "https://arti-dev.ss.aws.fwmrm.net/api/npm/fw-npm/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
  integrity sha1-w2GT3Tzhwu7SrbfIAtu8d6gbHA8=

moment& moment-timezonecould be used substitute for each other in this case.

momentmoment-timezone在这种情况下,&可以相互替代。

回答by Gadani

For Node.js, According to the original documentation: moment js documentation

对于 Node.js,根据原始文档: moment js 文档

You should do

你应该做

npm install moment-timezone

Then use it like this

然后像这样使用它

var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();

回答by John

Moment should be a function call. So use let myDate = moment().tz(...)

时刻应该是一个函数调用。所以用let myDate = moment().tz(...)

See https://momentjs.com/timezone/docs/for more details.

有关更多详细信息,请参阅https://momentjs.com/timezone/docs/

EDIT

编辑

You should also ensure that you are including the timezone extension to the moment library either through the correct npm install and require (for Node) or the correct script tags (for general browser usage). See the linked documents for the libraries/scripts to include.

您还应该确保通过正确的 npm install 和 require(对于 Node)或正确的脚本标签(对于一般浏览器使用)将时区扩展包含到 moment 库中。请参阅要包含的库/脚本的链接文档。