使用momentjs语言明智的动态的相对时间(Instagram风格)的缩写? [英] Abbreviated relative time (Instagram style) using momentjs language wise dynamic?
问题描述
moment().startOf('day').fromNow() //3 days ago.
如何更改以上内容以显示3d而不是英语和英语 如果语言是法语,则为"3 j",依此类推语言会动态变化吗?
How can I change the above to show 3d instead of language is English and to "3 j" if language is french and so on dynamically change according to the language?
推荐答案
您可以使用文档.请记住,您必须导入所需的所有语言环境,也可以使用提供所有受支持语言环境的moment-with-locales.js
.
You can use moment.locale
method as reported in the docs. Remember that you have to import all locales you need or you can use moment-with-locales.js
that provides all supported locales.
要自定义"momentjs" fromNow
方法的格式,可以使用自定义-> updateLocale ="nofollow">文档的相对时间部分.
To customize format of momentjs fromNow
method you can use updateLocale
as documented in the Customize -> Relative time section of the docs.
moment.updateLocale('en',{
relativeTime : {
future: "in %s",
past: "%s",
s: "seconds",
m: "a minute",
mm: "%d minutes",
h: "an hour",
hh: "%d h",
d: "a day",
dd: "%d d",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
moment.updateLocale('fr',{
relativeTime : {
future : 'dans %s',
past : '%s',
s : 'quelques secondes',
m : 'une minute',
mm : '%d minutes',
h : 'une heure',
hh : '%d heures',
d : 'un jour',
dd : '%d j',
M : 'un mois',
MM : '%d mois',
y : 'un an',
yy : '%d ans'
}
});
moment.locale('fr');
moment().subtract(2, 'days').startOf('day').fromNow() // 2 j
moment.locale('en');
moment().subtract(2, 'days').startOf('day').fromNow() // 2 d
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment-with-locales.js"></script>
这篇关于使用momentjs语言明智的动态的相对时间(Instagram风格)的缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!