2017-01-19 62 views
0

从文档设置格式l返回格式为“M/D/YYYY”。 有没有办法根据区域设置只取得D/MM/D格式的日期?这是我现在的代码。时刻格式化区域设置

var locale = window.navigator.userLanguage || window.navigator.language; 
moment.locale(locale); 
var momentTime = moment(d); 
console.log(momentTime.format('l')); 

例如如果语言环境是French那么日期应该在D/M格式返回,如果语言环境是english-us然后将日期应在M/D格式自动返回。

+0

[区域设置和特定的日期格式Moment.js](的可能的复制https://stackoverflow.com/questions/27360102/locale-and-specific-date-format-with -moment-JS) – SubjectDelta

回答

2

一种方法来做你所需要的是本地化longDateFormat,然后用正则表达式删除年份。

这里的一个工作示例,使用localeData,然后longDateFormat。我不确定它是否适用于每个区域,但它为fren-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>

相关问题