我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
当前回答
你需要时间。lang(警告:lang()自2.8.0时刻起已弃用,请使用locale()代替):
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
从v2.8.1开始,moment.locale('de')设置本地化,但不返回时刻。一些例子:
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实例设置locale,但不会返回moment实例。在实例上调用locale,为该实例设置它并返回该实例。
此外,正如Shiv在评论中所说,确保你使用的是“moment-with-locale .min.js”而不是“moment.min.js”,否则它将无法工作。
其他回答
在2.18.1秒之后:
moment.locale("de");
var m = moment().format("LLL")
你需要时间。lang(警告:lang()自2.8.0时刻起已弃用,请使用locale()代替):
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
从v2.8.1开始,moment.locale('de')设置本地化,但不返回时刻。一些例子:
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实例设置locale,但不会返回moment实例。在实例上调用locale,为该实例设置它并返回该实例。
此外,正如Shiv在评论中所说,确保你使用的是“moment-with-locale .min.js”而不是“moment.min.js”,否则它将无法工作。
我使用的是angular2-moment,但用法必须相似。
import { MomentModule } from "angular2-moment";
import moment = require("moment");
export class AppModule {
constructor() {
moment.locale('ru');
}
}
对于momentjs 2.12+,执行以下操作:
moment.updateLocale('de');
还要注意,你必须使用moment。updateLocale(localeName, config)来更改现有的语言环境。moment.defineLocale(localeName, config)应该只用于创建一个新的区域设置。
我不知道发生了什么变化,但像这样导入语言文件对我有用
import 'moment/src/locale/fr';
moment.locale('fr')
注意import语句中的src