如何根据用户语言格式化日期并显示月份和日期

编程入门 行业动态 更新时间:2024-10-25 13:24:42
本文介绍了如何根据用户语言格式化日期并显示月份和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试根据用户的浏览器语言偏好在javascript中显示日期和时间.我收到UTC格式的日期,并且通过使用toLocaleString()可以将其转换为浏览器时区.但是我还需要将日期名称和月份名称转换为浏览器语言.

I am trying to display the date and time in javascript based on users browser language preference. I am receiveing the date in UTC format and by using toLocaleString() i am able to convert it to browser time zone. But i also need to convert the day name and month name to browser language.

对于

2009/6/15下午1:45:30-> 2009年6月15日,星期一,下午8:45:30(zh-CN) 2009/6/15 1:45:30 PM-> 2009年6月15日20:45:30(sv-SE) 6/15/2009 1:45:30 PM->Δευτέρα,15Ιουνίου2009 8:45:30μμ(el-GR)

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 8:45:30 PM (en-US) 6/15/2009 1:45:30 PM -> den 15 juni 2009 20:45:30 (sv-SE) 6/15/2009 1:45:30 PM -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)

推荐答案

如果无论浏览器如何都想要一致的输出,moment.js是一个不错的选择.

If you want consistent output regardless of browser, moment.js is a good option.

// set the desired language moment.lang('sv'); // use one of the localized format strings var s = moment(yourDate).format('LLLL');

moment.js主页上有一些在线示例,其中显示了所有可用的语言.我不认为目前不支持希腊语,但是由于它是开源的,所以您随时可以自己添加.

There are live examples on the moment.js home page, showing all of the available languages. I don't believe there is currently support for Greek, but since it is open-source you could always add it yourself.

更多推荐

如何根据用户语言格式化日期并显示月份和日期

本文发布于:2023-10-23 00:03:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   语言   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!