[英]moment get date formatted on locale
From the moment docs setting format as l
returns in format "M/D/YYYY". 从文档设置格式为
l
的那一刻返回格式为“M / D / YYYY”。 Is there a way to get the date in just D/M
or M/D
format depending on locale? 有没有办法根据区域设置以
D/M
或M/D
格式获取日期? Here is the code i have right now. 这是我现在的代码。
var locale = window.navigator.userLanguage || window.navigator.language;
moment.locale(locale);
var momentTime = moment(d);
console.log(momentTime.format('l'));
For instance if the locale is French
then the date should be returned in D/M
format and if the locale is english-us
then the date should be returned in M/D
format automatically. 例如,如果语言环境是
French
则日期应以D/M
格式返回,如果语言环境为english-us
,则应自动以M/D
格式返回日期。
One way to do what you need is getting localized longDateFormat
and then remove the year part with a regular expression. 执行所需操作的一种方法是获取本地化的
longDateFormat
,然后使用正则表达式删除year部分。
Here a working example that uses localeData
and then longDateFormat
. 这里使用一个工作示例
localeData
,然后longDateFormat
。 I'm not sure that it will work for each locale, but it gives the right result for fr
and en-us
(and probably many others). 我不确定它是否适用于每个语言环境,但它为
fr
和en-us
(可能还有许多其他语言)提供了正确的结果。
var locale = window.navigator.userLanguage || window.navigator.language; moment.locale(locale); // Get locale data var localeData = moment.localeData(); var format = localeData.longDateFormat('L') // Remove year part format = format.replace(/.YYYY/, ''); var momentTime = moment(); console.log(momentTime.format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.