我正在尝试更改由时刻设置的日期的语言.js。默认是英语,但我想设置德语。这些是我尝试过的:
var now = moment().format("LLL").lang("de");
它给了NaN
.
var now = moment("de").format("LLL");
这甚至没有反应。
var now = moment().format("LLL", "de");
没有变化:这仍然产生英语结果。
这怎么可能?
你需要moment.lang(警告:lang()
从2.8.0
起就被弃用了,请改用locale()
(:
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
<小时 />从 v2.8.1 开始,moment.locale('de')
设置本地化,但不返回moment
。一些例子:
var march = moment('2017-03')
console.log(march.format('MMMM')) // 'March'
moment.locale('de') // returns the new locale, in this case 'de'
console.log(march.format('MMMM')) // 'March' still, since the instance was before the locale was set
var deMarch = moment('2017-03')
console.log(deMarch.format('MMMM')) // 'März'
// You can, however, change just the locale of a specific moment
march.locale('es')
console.log(march.format('MMMM')) // 'Marzo'
在求和中,在全局moment
上调用 locale
会为所有将来的 moment
实例设置区域设置,但不返回 moment
的实例。在实例上调用 locale
,为该实例设置该实例并返回该实例。
另外,正如Shiv在评论中所说,请确保使用"moment-with-locales.min.js"而不是"moment.min.js",否则它将不起作用。
我还必须导入语言:
import moment from 'moment'
import 'moment/locale/es' // without this line it didn't work
moment.locale('es')
然后像往常一样使用瞬间
console.log(moment(date).fromNow())
最快的方法:使用 Bower 安装
我刚刚安装了带有bower的时刻,并将de.js
链接为我的html项目中的javascript资源。
bower install moment --save
您也可以手动下载moment.js
和de.js
。
在项目中链接"de.js">
在我的主项目文件中链接de.js
会自动更改对 moment 类及其方法的所有访问的区域设置。
将不再需要做moment.locale("de").
或 moment.lang("de").
源代码中。
只需像这样链接您想要的区域设置:
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
或者,如果您通过右键单击下载了 moment.js 1990 年代风格的 Moment,则可以在没有bower_components
路径的情况下链接库,这在大多数情况下仍然可以正常工作。
使用 momentjs 2.8+,执行以下操作:
moment.locale("de").format('LLL');
http://momentjs.com/docs/#/i18n/
扎后,这对我moment
v2.26.0 有用:
import React from "react";
import moment from "moment";
import frLocale from "moment/locale/fr";
import esLocale from "moment/locale/es";
export default function App() {
moment.locale('fr', [frLocale, esLocale]) // can pass in 'en', 'fr', or 'es'
let x = moment("2020-01-01 00:00:01");
return (
<div className="App">
{x.format("LLL")}
<br />
{x.fromNow()}
</div>
);
}
您可以传入en
、fr
或es
。如果你想要另一种语言,你必须导入区域设置并将其添加到数组中。
如果您只需要支持一种语言,则更简单一些:
import React from "react";
import moment from "moment";
import "moment/locale/fr"; //always use French
export default function App() {
let x = moment("2020-01-01 00:00:01");
return (
<div className="App">
{x.format("LLL")}
<br />
{x.fromNow()}
</div>
);
}
--更新--
在一种情况下,导入上述所有语言环境文件将采用始终使用的最后一个导入的语言环境(即上面示例中的"es"(,即使我将其设置为"fr"。 似乎有效的解决方案是:
import moment from 'moment';
import 'moment/min/locales';
...
moment.locale('fr');
2017/2018 年底:其他人的答案有太多的旧代码需要编辑,所以这里是我的替代干净答案:
与要求
let moment = require('moment');
require('moment/locale/fr.js');
// or if you want to include all locales:
require("moment/min/locales.min");
与进口
import moment from 'moment';
import 'moment/locale/fr';
// or if you want to include all locales:
require("moment/min/locales.min");
用:
moment.locale('fr');
moment().format('D MMM YY'); // Correct, set default global format
// moment.locale('fr').format('D MMM YY') //Wrong old versions for global default format
带时区
*需要:
require('moment-range');
require('moment-timezone');
*进口:
import 'moment-range';
import 'moment-timezone';
使用区域:
const newYork = moment.tz("2014-06-01 12:00", "America/New_York");
const losAngeles = newYork.clone().tz("America/Los_Angeles");
const london = newYork.clone().tz("Europe/London");
格式化日期的功能
const ISOtoDate = function (dateString, format='') {
// if date is not string use conversion:
// value.toLocaleDateString() +' '+ value.toLocaleTimeString();
if ( !dateString ) {
return '';
}
if (format ) {
return moment(dateString).format(format);
} else {
return moment(dateString); // It will use default global format
}
};
在脚本中添加moment.lang(navigator.language)
。
并且还必须添加要在其中显示的每个国家/地区区域设置:例如,对于 GB 或 FR,您需要立即添加该区域设置格式.js库。这种格式的示例可以在 momentjs 文档中找到。如果您不立即添加此格式.js那么它总是会选择美国区域设置,因为这是我目前看到的唯一一种。
对于 METEOR 用户:
默认情况下不会在 meteor 中安装时刻语言环境,您只会在默认安装中获得"en"语言环境。
因此,您可以使用其他答案中正确显示的代码:
moment.locale('it').format('LLL');
但它将保持英文,直到您安装所需的语言环境。
有一种很好、干净的方式来在 meteor 中添加单独的语言环境(由 rzymek 提供(。
以通常的流星方式安装力矩包:
meteor add rzymek:moment
然后只需添加您需要的语言环境,例如意大利语:
meteor add rzymek:moment-locale-it
或者,如果您真的想添加所有可用的语言环境(向页面添加大约 30k(:
meteor add rzymek:moment-locales
随着时刻 2.18.1 及以后:
moment.locale("de");
var m = moment().format("LLL")
我不确定发生了什么变化,但像这样导入语言文件对我有用
import 'moment/src/locale/fr';
moment.locale('fr')
注意导入语句中的 src
这样工作正常: return moment(status.created_at).locale('es').fromNow();
这个只是通过自动检测当前用户位置来工作。
import moment from "moment/min/moment-with-locales";
// Then use it as you always do.
moment(yourDate).format("MMMM Do YYYY, h:mm a")
对于 momentjs 2.12+,请执行以下操作:
moment.updateLocale('de');
另请注意,必须使用 moment.updateLocale(localeName, config)
来更改现有区域设置。 moment.defineLocale(localeName, config)
应仅用于创建新区域设置。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MomentJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script type="text/javascript" src="moment.js"></script>
<script type="text/javascript" src="locale/ne.js"></script>
</head>
<body>
<script>
jQuery(document).ready(function($) {
moment.locale('en'); // default the locale to English
var localLocale = moment();
moment.locale('ne'); // change the global locale to Nepalese
var ne1 = localLocale.format('LLLL');
var ne2 = moment().format('LLLL');
$('.ne1').text(ne1);
$('.ne2').text(ne2);
});
</script>
<p class="ne1"></p>
<p class="ne2"></p>
</body>
</html>
演示
当我与 gulp 和朋友一起使用 webpack 时(这个生成器为我设置了一切(,我不得不对 bower.json 文件进行更改。我必须覆盖当前包的默认导入并选择所有语言附带的文件:
"overrides": {
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
}
这是我的完整bower.json文件:
{
"name": "html5",
"version": "0.0.0",
"dependencies": {
"angular-animate": "~1.4.2",
"angular-cookies": "~1.4.2",
"angular-touch": "~1.4.2",
"angular-sanitize": "~1.4.2",
"angular-messages": "~1.4.2",
"angular-ui-router": "~0.2.15",
"bootstrap-sass": "~3.3.5",
"angular-bootstrap": "~0.13.4",
"malarkey": "yuanqing/malarkey#~1.3.1",
"angular-toastr": "~1.5.0",
"moment": "~2.10.6",
"animate.css": "~3.4.0",
"angular": "~1.4.2",
"lodash": "^4.13.1",
"angular-moment": "^0.10.3",
"angularLocalStorage": "ngStorage#^0.3.2",
"ngstorage": "^0.3.10"
},
"devDependencies": {
"angular-mocks": "~1.4.2"
},
"overrides": {
"bootstrap-sass": {
"main": [
"assets/stylesheets/_bootstrap.scss",
"assets/fonts/bootstrap/glyphicons-halflings-regular.eot",
"assets/fonts/bootstrap/glyphicons-halflings-regular.svg",
"assets/fonts/bootstrap/glyphicons-halflings-regular.ttf",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff2"
]
},
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
},
"resolutions": {
"angular": "~1.4.2"
}
}
<</div>
div class="one_answers"> 我正在使用 angular2-moment,但用法必须相似。
import { MomentModule } from "angular2-moment";
import moment = require("moment");
export class AppModule {
constructor() {
moment.locale('ru');
}
}
根据版本更改 js 语言的时刻
版本: 2.8+
moment.locale('hi'(;
版本: 2.5.1
moment.lang('hi'(;
,需要进行一些更改(版本 2.20(
- 使用
moment.locale('de')
设置区域设置,然后创建一个表示现在日期的新对象(moment()
(注意括号(,然后format('LLL')
它。括号很重要。
所以这意味着:
moment.locale('de');
var now = moment();
now.format('LLL');
- 另外,请记住使用
moment-with-locale.js
。该文件包含所有区域设置信息,并且文件大小较大。下载locale
文件夹是不够的。如有必要,请将名称更改为moment.js
。Django 只是拒绝在我的情况下加载moment-with-locale.js
。
编辑:事实证明,重命名文件是不必要的。我只是忘了在页面中调用它,所以 Django 认为没有必要加载它,所以我的错。
哎呀呀�我会解决这个问题: var moment = function(x) { return moment(x).locale('de'); }
其他方式似乎并没有真正坚持/保持条件(对我来说(。
对于在异步环境中工作的用户,moment
在按需加载区域设置时行为异常。
而不是
await import('moment/locale/en-ca');
moment.locale('en-ca');
颠倒顺序
moment.locale('en-ca');
await import('moment/locale/en-ca');
似乎区域设置已加载到当前选定的区域设置中,覆盖了以前设置的任何区域设置信息。因此,先切换区域设置,然后加载区域设置信息不会导致此问题。
要使用moment(版本高于2.8.0(更改区域设置,请执行以下步骤。
-
在索引中加载时刻语言环境文件.html如下所示
<script src="../node_modules/moment/locale/it.js"></script>
-
根据需要设置区域设置 -
moment.locale('it')
; -
现在
moment.locale()
将返回"它"> -
您可以将 moment 与任何语言一起使用,例如 - JavaScript、Angular、node 等。
First Call , p5.js 和 moment-with-locales.js然后像下面这样编写代码,你会得到你的结果。
在此结果中,我以不同的语言显示了月份名称:)
请检查代码 :
var monthNameEnglish = moment().locale('en-gb').format('MMMM');
document.getElementById('monthNameEnglish').innerHTML = monthNameEnglish;
var monthNameGerman = moment().locale('de').format('MMMM');
document.getElementById('monthNameGerman').innerHTML = monthNameGerman;
<!DOCTYPE html>
<html>
<head>
<title>P5.js and Moment.js</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment-with-locales.js"></script>
<h3>English Version Month Name</h3>
<p id="monthNameEnglish"></p>
<h3> German Version Month Name</h3>
<p id="monthNameGerman"></p>
</head>
<body>
</body>
</html>
我们可以使用 moment.locale(( 方法来传递你自己的语言,例如。我正在使用window.navigator.language(ex.en-US(在运行时传递lange。
const formatDate = date => moment(date).locale(window.navigator.language).format('LL')
请尝试:
从"时刻/时刻"导入时刻;
导入"时刻/区域设置/ES";
我从 https://es.stackoverflow.com/questions/117997/c%C3%B3mo-utilizar-momentjs-con-fechas-en-espa%C3%B1ol 中找到了解决方案