星期几和月份的JavaScript全名(或如何获取每个客户区域设置的日期名) [英] JavaScript full names for day-of-week and month (or How to get the date names per client locale)

查看:56
本文介绍了星期几和月份的JavaScript全名(或如何获取每个客户区域设置的日期名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(new Date())。toString()返回的字符串看起来像这样:

The string that is returned from (new Date()).toString() looks something like this:

"Tue Nov 22 2016 14:14:51 GMT-0800 (Pacific Standard Time)"

是否存在我们可以使用的内置方法/构造函数,而不会缩短星期几和/或月份?换句话说,JS是否支持:

Is there a built-in method/constructor that we can use that will not abbreviate the day-of-week and/or month? In other words, does JS support this:

"Tuesday November 22 2016 14:14:51 GMT-0800 (Pacific Standard Time)"

我之所以提出这个问题,是因为我正在寻找一种懒惰/骇人的方式,也许可以使用客户端的语言来获取工作日和月份名称。

The reason for my question is because I'm looking for a lazy/hacky way that we might be able to get the Weekday and Month names in the language of the client.

推荐答案

如果不需要支持旧版浏览器(早于IE 11),则可以使用 toLocalDateString()

If you don't need to support old browsers (older than IE 11), you can use toLocalDateString().

示例:

new Date().toLocaleDateString('en-US', {
    weekday: 'long',
    month: 'long',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timeZoneName: 'short'
})

但是 moment.js 更加舒适。

请参见 MDN Date.prototype.toLocaleDateString()了解更多信息。

这篇关于星期几和月份的JavaScript全名(或如何获取每个客户区域设置的日期名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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