don*_*yor 166 javascript momentjs
我正在尝试更改由moment.js设置的日期语言.默认一个是英语,但我想用德语.这些是我试过的:
var now = moment().format("LLL").lang("de");
Run Code Online (Sandbox Code Playgroud)
给予NAN,没有用.
var now = moment("de").format("LLL");
Run Code Online (Sandbox Code Playgroud)
甚至没有反应,也没有起作用.
var now = moment().format("LLL", "de");
Run Code Online (Sandbox Code Playgroud)
没有变化,仍然英语,没有工作.
这怎么可能?
kal*_*ley 251
你需要moment.lang(警告:lang()从那时起就弃用了2.8.0,locale()改为使用):
moment.lang("de").format('LLL');
Run Code Online (Sandbox Code Playgroud)
http://momentjs.com/docs/#/i18n/
从v2.8.1开始,moment.locale('de')设置本地化,但不返回a 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'
Run Code Online (Sandbox Code Playgroud)
总之,调用locale全局moment设置所有未来moment的语言环境,但不返回实例moment.调用locale一个实例,为该实例设置AND并返回该实例.
Agu*_*ndo 120
我还必须导入语言:
import moment from 'moment'
import 'moment/locale/es' // without this line it didn't work
moment.locale('es')
Run Code Online (Sandbox Code Playgroud)
然后像往常一样使用时刻
alert(moment(date).fromNow())
Run Code Online (Sandbox Code Playgroud)
mes*_*lds 55
我刚用凉亭安装了一下,并de.js在我的html项目中作为javascript资源链接.
bower install moment --save
您也可以手动下载moment.js和de.js.
链接de.js我的主项目文件会自动更改当前类及其方法的所有访问的语言环境.
将有 没有必要再去做一个moment.locale("de").或者
moment.lang("de"). 源代码.
只需链接您想要的语言环境,如下所示:
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
Run Code Online (Sandbox Code Playgroud)
或者你可以在没有bower_components路径的情况下链接库,如果你通过右键单击下载了moment.js 1990ies-style,这在大多数情况下仍然可以正常工作.
Nas*_*nas 35
使用momentjs 2.8+,执行以下操作:
moment.locale("de").format('LLL');
Run Code Online (Sandbox Code Playgroud)
http://momentjs.com/docs/#/i18n/
Sma*_*der 10
您需要添加moment.lang(navigator.language)脚本.
并且还必须添加要显示的每个国家/地区区域设置:例如,对于GB或FR,您需要在moment.js库中添加该区域设置格式.momentjs文档中提供了此类格式的示例.如果你不在moment.js中添加这种格式,那么它总是会选择美国语言环境,因为这是我目前唯一看到的语言环境.
sta*_*ave 10
2017/2018结束:anothers回答有太多旧代码要编辑,所以这里我的替代干净答案:
有要求
let moment = require('moment');
require('moment/locale/fr.js');
// or if you want to include all locales:
require("moment/min/locales.min");
Run Code Online (Sandbox Code Playgroud)
与进口
import moment from 'moment';
import 'moment/locale/fr';
// or if you want to include all locales:
require("moment/min/locales.min");
Run Code Online (Sandbox Code Playgroud)
使用:
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
Run Code Online (Sandbox Code Playgroud)
与时区
*要求:
require('moment-range');
require('moment-timezone');
Run Code Online (Sandbox Code Playgroud)
*进口:
import 'moment-range';
import 'moment-timezone';
Run Code Online (Sandbox Code Playgroud)
使用区域:
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");
Run Code Online (Sandbox Code Playgroud)
用于格式化日期
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
}
};
Run Code Online (Sandbox Code Playgroud)
经过挣扎,这对我来说适用于momentv2.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>
);
}
Run Code Online (Sandbox Code Playgroud)
您可以传入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>
);
}
Run Code Online (Sandbox Code Playgroud)
对于美国用户:
默认情况下,在meteor中没有安装时刻区域设置,只能使用默认安装获得"en"语言环境.
因此,您可以在其他答案中正确使用代码:
moment.locale('it').format('LLL');
Run Code Online (Sandbox Code Playgroud)
但在您安装所需的语言环境之前,它将保留为英语.
有添加单个语言环境的一个很好的,干净的方式瞬间的流星(通过提供rzymek).
使用通常的流星方式安装力矩包:
meteor add rzymek:moment
Run Code Online (Sandbox Code Playgroud)
然后只需添加您需要的语言环境,例如意大利语:
meteor add rzymek:moment-locale-it
Run Code Online (Sandbox Code Playgroud)
或者,如果您确实要添加所有可用的区域设置(为您的页面添加大约30k):
meteor add rzymek:moment-locales
Run Code Online (Sandbox Code Playgroud)
随着时刻 2.18.1 及以后:
moment.locale("de");
var m = moment().format("LLL")
Run Code Online (Sandbox Code Playgroud)
这只是通过自动检测当前用户位置来工作。
import moment from "moment/min/moment-with-locales";
// Then use it as you always do.
moment(yourDate).format("MMMM Do YYYY, h:mm a")
Run Code Online (Sandbox Code Playgroud)
我不确定发生了什么变化,但导入这样的语言文件对我有用
import 'moment/src/locale/fr';
moment.locale('fr')
Run Code Online (Sandbox Code Playgroud)
注意 import 语句中的 src