考虑在以下位置运行相同的 javascript 应用程序:
- 浏览器(主要是 Chrome):
Intl.DateTimeFormat().format(new Date)//"05/01/2018"
- Electron :
Intl.DateTimeFormat().format(new Date)//"2018-1-5"
我们有一个项目列表,每个项目都有一个字段:创建日期
,它基本上以语言环境格式显示日期:
const items = [
{
title: 'I am the Title',
createdDate: new Date(),
id: 'foobazID'
}
];
const render = item => (`
<table class="table table-striped" id="${item.id}">
<tr>
<th>ID:</th><td>${item.id}</td>
</tr>
<tr>
<th>Title:</th><td>${item.title}</td>
</tr>
<tr>
<th>Created At:</th><td>${item.createdDate.toLocaleDateString()}</td>
</tr>
</table>
`);
document
.getElementById('view')
.innerHTML = items.reduce((res, item) => res.concat(render(item)), '')
;
#view {
margin: 10px;
padding: 10px;
border: 1px solid #dee2e6;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css">
<section id="view">
</section>
现在,考虑到在同一台机器上运行的两个应用程序,我们得到了两种不同的日期时间格式:
Electron :
谷歌浏览器:
我们可以做些什么来获得相同的格式输出?
最佳答案
您可能首先想在 Electron 和 Chrome 各自的 DevTools 控制台中运行以下语句,以发现默认日期时间格式的任何差异:
Intl.DateTimeFormat().resolvedOptions()
很可能 locale
属性具有不同的值("en"
、"en-UK"
、"en -US"
等)基于某些浏览器设置...
然后,将适当的 locale
作为参数显式添加到 Intl.DateTimeFormat , "en"
例如:
Intl.DateTimeFormat("en").format(new Date)
关于javascript - Date#toLocaleDateString Chrome 与 Electron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112549/