如何在JavaScript中检索用户操作系统首选日期和时间格式

时间:2017-10-05 22:29:09

标签: javascript html google-chrome date operating-system

我注意到使用下面的html代码:

<input type="date" value="2017-12-13" />

输入结果如下:

enter image description here

此格式与Windows格式配置一致: enter image description here

但是我没有发现任何证据表明我可以使用javascript函数手动格式化日期。

他们在html元素中提供此功能似乎很奇怪,但在javascript中却没有。

作为一种hacky方法,我已经研究过只创建一个输入,禁用它并使用css将其格式化为简单文本。

是否有我不知道使用此格式的功能,如果没有,是否有任何计划添加此功能。

要轻松测试自己,请按照上图中指示的窗口中的格式更改进行操作。重新启动Google Chrome,然后访问https://jsfiddle.net/3hyyv04d/1/

2 个答案:

答案 0 :(得分:1)

我认为您没有错过任何核心功能,并且在撰写本文时没有计划添加它,因为RFC现在已经关闭。 本主题包含所有必要信息:Is there any way to change input type="date" format?

答案 1 :(得分:1)

这里已经回答了类似的问题。 How to get the exact local time of client?此处How to find the operating system version using JavaScript以及How to detect my browser version and operating system using JavaScript?
检测操作系统:

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);


// 2. To know the timezone of the client relative to GMT/UTC here you go:

    var d = new Date();
    var tz = d.toString().split("GMT")[1].split(" (")[0]; // timezone 

    var d = new Date();
    var tz = d.toString().split("GMT")[1]; // timezone, i.e. -0700 (Pacific Daylight Time)

// This script sets OSName variable as follows: // "Windows" for all versions of Windows // "MacOS" for all versions of Macintosh OS // "Linux" for all versions of Linux // "UNIX" for all other UNIX flavors // "Unknown OS" indicates failure to detect the OS var OSName="Unknown OS"; if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; document.write('Your OS: '+OSName); // 2. To know the timezone of the client relative to GMT/UTC here you go: var d = new Date(); var tz = d.toString().split("GMT")[1].split(" (")[0]; // timezone var d = new Date(); var tz = d.toString().split("GMT")[1]; // timezone, i.e. -0700 (Pacific Daylight Time)