2016-09-15 160 views
-1

我在Moment.js中正確格式化日期時遇到問題。我使用格式爲「LLL D,YYYY」的格式函數,因此它應該返回類似於「2016年9月15日」的內容。如何在Moment.js中格式化日期

取而代之的是,它以一種奇怪的格式返回日期,如「2016年9月15日12:00 AM 15,2016」。

這是我的代碼,下面有調試信息。

moment.locale(picker.options.language); 

console.log('picker.options.language:'); 
console.log(picker.options.language); 

formatted = moment(picker.date).format(picker.format); 

console.log('picker.date:'); 
console.log(picker.date); 

console.log('picker.format:'); 
console.log(picker.format); 

console.log('formatted:'); 
console.log(formatted); 

而且從上面的代碼輸出控制檯:

debugging output

回答

2

http://momentjs.com/docs/#/displaying/format/我們可以看到,「LLL」表示格式「月名,月,年,時間一天。」你似乎想要「月日,年」,即「LL」。

嘗試:

picker.format = 'LL'; 
formatted = moment(picker.date).format(picker.format); 
console.log(formatted); 

輸出(今天的日期):

September 15, 2016 
+0

除其實有對於'LL'我不是昏迷謝謝 – user3

+0

謝謝。這種格式來自PHP的IntlDateFormatter :: defaultDateFormats [self :: MEDIUM]。 – amacrobert