如何根据用户语言格式化日期并显示月份和日期 [英] How to format date and display month and day based on user language

查看:99
本文介绍了如何根据用户语言格式化日期并显示月份和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的浏览器语言偏好在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.

这篇关于如何根据用户语言格式化日期并显示月份和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆