javascript - Date#toLocaleDateString Chrome 与 Electron

标签 javascript google-chrome date localization electron

考虑在以下位置运行相同的 javascript 应用程序:

  1. 浏览器(主要是 Chrome):Intl.DateTimeFormat().format(new Date)//"05/01/2018"
  2. 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 Version

谷歌浏览器:

enter image description here

我们可以做些什么来获得相同的格式输出?

最佳答案

您可能首先想在 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/

相关文章:

Java String to Date 格式为 “MM/dd/yyyy HH:mm:ss a” 的对象

javascript - 页眉和页脚之间的随机响应背景图像?

javascript - AngularJS 中的 Phonegap ondeviceready 事件

javascript - SystemJS 找不到带有 Typescript 和 Electron 的 xml-js 模块

javascript - Magnific Popup 不显示

css - 自定义复选框在 Firefox 上不起作用

javascript - Chrome javascript 控制台行为奇怪

javascript - AudioContext() 的多个来源

c# - 使用 asp.net mvc 计算相对日期

ruby-on-rails - 访问从 Rails 中的日期助手发送的 date() 变量