[英]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.