[英]Date format - momentjs - extract date and time separately using locale language
I am using moment.js library I have a date in this format : 我正在使用moment.js库我有这种格式的日期:
2014-08-07T10:00:00+02:00
I want to have two separate values : 我想要两个单独的值:
- Thursday, August 7 2014
- 10 am
But I also want them to be using the local language. 但我也希望他们使用当地语言。 For example, if moment.lang("fr"), the output should be
例如,如果是moment.lang(“fr”),则输出应为
- Jeudi 7 Août 2014
- 10h
I set the moment.js lang in the correct way. 我以正确的方式设置了moment.js lang。 I managed to remove hour,minutes and seconds (to extract the first value) :
我设法删除小时,分钟和秒(以提取第一个值):
new Date(moment.utc(date).format('LL')) //Outputs Thu Aug 07 2014 00:00:00 GMT+0200 (Paris, Madrid)
But I don't know how to extract the hour and minutes (for the second value) and how to show the date using the current language . 但我不知道如何提取小时和分钟 (第二个值)以及如何使用当前语言显示日期。
@Rayjax solution is not working anymore in recent versions of moment.js. @Rayjax解决方案在最新版本的moment.js中不再有效。 Behavior of
moment.localeData().longDateFormat()
changed. moment.localeData().longDateFormat()
行为已更改。 LT
is now already replaced by time format. LT
现在已经被时间格式所取代。 Instead of dddd, MMMM D, YYYY LT
it returns now dddd, MMMM D, YYYY h:mm A
. 而不是
dddd, MMMM D, YYYY LT
它现在返回dddd, MMMM D, YYYY h:mm A
Therefore removing LT
from string does not work anymore. 因此,从字符串中删除
LT
不再起作用。 But we could just remove compiled LT
for current locale: 但我们可以删除当前语言环境的已编译
LT
:
moment.localeData().longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim();
Trim is necessary to avoid unnecessary white spaces. 修剪是必要的,以避免不必要的空格。
Hi I don't know if it's the best way do to it but it's working 嗨,我不知道这是不是最好的方式,但它的工作
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('.' , '')))
http://jsfiddle.net/yann86/tb0u5eav/ http://jsfiddle.net/yann86/tb0u5eav/
I came up with this which works well : 我想出了这个很好用的:
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
What I was missing were the language configs files : http://momentjs.com/ i grabbed momentjs+locales instead of moment.js only and it worked. 我缺少的是语言配置文件: http ://momentjs.com/我抓住了momentjs + locales而不是moment.js而且它有效。
It is important to note that moment.js won't tell you you are missing those filed, it will just default to english. 重要的是要注意,moment.js不会告诉你缺少那些提交的,它只会默认为英文。
It worked for my needs: 它符合我的需求:
moment().locale('en').format('L');
moment().locale('pt-br').format('L');
Just change the format for your needs. 只需根据需要更改格式即可。 The Documentation of the moment is great.
当下的文档很棒。 http://momentjs.com/
http://momentjs.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.