2017-01-19 66 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>

相關問題