[英]Date format - momentjs - extract date and time separately using locale language
我正在使用moment.js庫我有這種格式的日期:
2014-08-07T10:00:00+02:00
我想要兩個單獨的值:
- Thursday, August 7 2014
- 10 am
但我也希望他們使用當地語言。 例如,如果是moment.lang(“fr”),則輸出應為
- Jeudi 7 Août 2014
- 10h
我以正確的方式設置了moment.js lang。 我設法刪除小時,分鍾和秒(以提取第一個值):
new Date(moment.utc(date).format('LL')) //Outputs Thu Aug 07 2014 00:00:00 GMT+0200 (Paris, Madrid)
但我不知道如何提取小時和分鍾 (第二個值)以及如何使用當前語言顯示日期。
@Rayjax解決方案在最新版本的moment.js中不再有效。 moment.localeData().longDateFormat()
行為已更改。 LT
現在已經被時間格式所取代。 而不是dddd, MMMM D, YYYY LT
它現在返回dddd, MMMM D, YYYY h:mm A
因此,從字符串中刪除LT
不再起作用。 但我們可以刪除當前語言環境的已編譯LT
:
moment.localeData().longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim();
修剪是必要的,以避免不必要的空格。
嗨,我不知道這是不是最好的方式,但它的工作
moment.locale("en");
var now = moment()
console.log(now.format(moment.localeData().longDateFormat('LLLL').replace('LT' , '')));
console.log(now.format(moment.localeData().longDateFormat('LT').replace('mm' , '').replace(':' , '').replace('.' , '')))
我想出了這個很好用的:
moment.lang("fr"); //en, es or whatever
var date = moment(dateParam).format("LL"),
time = moment(dateParam).format("LT");
console.log(date, time) -- outputs separatedly date and time in the correct language form
我缺少的是語言配置文件: http ://momentjs.com/我抓住了momentjs + locales而不是moment.js而且它有效。
重要的是要注意,moment.js不會告訴你缺少那些提交的,它只會默認為英文。
它符合我的需求:
moment().locale('en').format('L');
moment().locale('pt-br').format('L');
只需根據需要更改格式即可。 當下的文檔很棒。 http://momentjs.com/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.