2015-01-09 20 views
0

我使用MomentJS v2.8.4,而我試圖讓格式化的日期,如「31/12/2015」MomentJS如何獲得格式化,但沒有本地化的表示?

myDate.format('DD/MM/YYYY')正常工作,直到我把一些「不太英語:)」的定位,例如阿拉伯。然後我得到這樣的١٠/٠١/٢٠١٥,這對用戶來說很好,對API不太好。

從MomentJS源代碼

format : function (inputString) { 
    var output = formatMoment(this, inputString || moment.defaultFormat); 
    // here I get correct "31/12/2015" format 
    return this.localeData().postformat(output); // this will return localized version 
}, 

formatMoment功能不公開導出...

能否請您提出這個正確的解決方案?

+0

將本地化設置爲英文,或者您可能有某些原因希望使用本地化? – Xotic750

+0

分享一些更多的代碼,而不是來自momentjs庫的代碼片段。 – Xotic750

+0

你最終選擇了什麼解決方案? –

回答

1

你可以保存在一個變量的當前locale()設置(即保存用戶的設置),然後明確設置locale值,這樣就可以讓你的日期格式正確爲您的API調用,然後設置locale值回保存的價值。

是這樣的:

var userLocaleSetting = moment.locale(); 
moment.locale('en'); 
var myFormattedDate = myDate.format('DD/MM/YYYY'); 
moment.locale(userLocaleSetting); 
0

一種解決方案可以是使用API​​和用戶格式化的日期返回對象。

format : function (inputString) { 
    var api = formatMoment(this, inputString || moment.defaultFormat); 
    // here I get correct "31/12/2015" format 
    var user = this.localeData().postformat(api); // this will return localized version 

    return {api: api, user: user}; 
}, 
+0

這將工作,但我不想分叉和編輯這樣已知的庫的api作爲momentjs – Strajk