下面是一个例子 日期toLocaleDateString() 方法
null
- 例子:
<
script
>
var dateObj = new Date();
var options = { weekday: "long",
year: "numeric",
month: "short",
day: "numeric" };
document.write(dateObj
.toLocaleDateString("en-US"));
document.write("<
br
>");
document.write(dateObj
.toLocaleDateString("en-US", options));
</
script
>
- 输出:
6/24/2018 Sunday, Jun 24, 2018
这个 日期toLocaleDateString() 方法用于将日期转换为字符串。 语法:
dateObj.toLocaleDateString( [locales][, options])
参数: 该方法接受两个参数,如下所述:
- 地区: 此参数是包含一个或多个语言或区域设置标记的区域设置字符串数组。请注意,它是一个可选参数。如果希望在应用程序的语言中使用该语言,请指定该语言。
- 选项: 它也是一个可选参数,包含指定比较选项的属性。一些属性包括localeMatcher、timeZone、weekday、year、month、day、hour、minute、second等。
返回值: 它以由语言环境指定的特定格式作为字符串值返回日期。
注: 这个 dateObj 应该是有效的日期对象。
以上方法的更多代码如下: 项目1: 如果没有参数,脚本中无法依赖此方法的返回值。它使用操作系统的区域设置约定。
< script > var dateObj = new Date(1993, 6, 28, 14, 39, 7); document.write(dateObj.toLocaleDateString()); </ script > |
输出:
7/28/1993
注: 并非所有浏览器都支持区域设置和选项参数。要检查它是否受支持,我们可以使用以下功能:
function toLocaleDateStringSupportsLocales() { try { new Date().toLocaleDateString('i'); } catch (e) { return e.name === 'RangeError'; } return false; }
支持的浏览器: 支持的浏览器 JavaScript Date toLocaleDateString()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 5.5及以上版本
- 歌剧5及以上
- Safari 1及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END