简体   繁体   English

Javascript来自字符串的日期,提供从Chrome到Firefox的不同输出

[英]Javascript Date from string giving different output from Chrome to Firefox

I'm trying to write some javascript code to format a date as I want, but I have troubles making it work on Firefox (it's working as I want on Chrome). 我正在尝试编写一些javascript代码来格式化我想要的日期,但是我有麻烦使它在Firefox上工作(它在Chrome上工作正常)。

The input I have in a form is 05/01/13 (mm/dd/yy) and I want 2013-05-01 (yyyy/mm/dd). 我在表格中的输入是05/01/13 (mm / dd / yy),我想要2013-05-01 (yyyy / mm / dd)。

For that, what I did is something like this : 为此,我所做的是这样的:

var formDate = document.getElementById("start").value;
var myDate = new Date(formDate);
var startDate = new Date();

startDate.setMonth(myDate.getMonth() + 1);
startDate.setFullYear(myDate.getFullYear());
var FormattedDate = startDate.getFullYear() + "-" + ((startDate.getMonth() <= 10) ? "0" : "") + startDate.getMonth() + "-01"; // the day is always 01

alert(FormattedDate);

You can try it on both browsers here : http://jsfiddle.net/j4BLH/ 您可以在这两种浏览器上试用它: http//jsfiddle.net/j4BLH/

On Google Chrome, this code gives me for example 2013-05-01 for May, but on Firefox, I have 1913-05-01 . 在谷歌浏览器上,此代码为我提供了例如2013-05-01 for May,但在Firefox上,我有1913-05-01

I know I could've written something like "20" + startDate.getYear() but I was wondering why the results were different from Chrome to Firefox ? 我知道我可以编写类似"20" + startDate.getYear()但我想知道为什么Chrome到Firefox的结果有所不同? And maybe if you have a better way of writing the code I pasted here, please let me know :) 也许如果你有更好的方法来编写我粘贴在这里的代码,请告诉我:)

Thanks ! 谢谢 !

From the spec : 规格

However, the expression Date.parse(x.toLocaleString()) is not required to produce the same Number value as the preceding three expressions and, in general, the value produced by Date.parse is implementation-dependent when given any String value that does not conform to the Date Time String Format (15.9.1.15) and that could not be produced in that implementation by the toString or toUTCString method. 但是,表达式Date.parse(x.toLocaleString())不需要生成与前三个表达式相同的Number值,并且通常,当给定任何String值时,Date.parse生成的值与实现有关。不符合日期时间字符串格式(15.9.1.15),并且不能通过toString或toUTCString方法在该实现中生成。

When creating a date object by passing a date string to the constructor, the method of parsing is exactly the same as Date.parse. 通过将日期字符串传递给构造函数来创建日期对象时,解析方法与Date.parse完全相同。

Your date format, using a 2 digit year, is not conforming to the standard. 您的日期格式使用2位数年份,不符合标准。 As such, it would seem that the way these dates are parsed is implementation specific, meaning that it depends upon the Javascript engine being used. 因此,似乎解析这些日期的方式是特定于实现的,这意味着它取决于所使用的Javascript引擎。 In Google's case, it's usually V8, and in Firefox, it's TraceMonkey or Rhino I believe. 在Google的情况下,它通常是V8,而在Firefox中,我认为它是TraceMonkey或Rhino。

In short, you should really use 4 digit years, as there is no YY in the JS standard. 简而言之,您应该使用4位数年份,因为JS标准中没有YY。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PhantomJS和Google Chrome / Firefox的HTML输出不同 - HTML output from PhantomJS and Google Chrome/Firefox are different 为什么Date.prototype.getTime()为Chrome和Firefox提供不同的值? - Why is Date.prototype.getTime() giving different values for Chrome and Firefox? javascript在IE 8中从字符串转换为日期后给出错误的日期 - javascript giving wrong date after conversion from string to date in IE 8 setInterval与chrome和firefox不同 - setInterval different from chrome and firefox Chrome和Firefox在CSS变量的JavaScript文件导入中给出了不同的结果 - Chrome and Firefox giving different results on JavaScript file import of CSS variable Date.getUTCHours() - Chrome,Firefox和Internet Explorer中的不同输出 - Date.getUTCHours() - Different output in Chrome, Firefox and Internet Explorer 为什么新的Date()函数在chrome和firefox中提供不同的输出 - why new Date() function gives different output in chrome and firefox 从JavaScript中的不同字符串格式读取日期 - Reading Date From Different String Formats in JavaScript 与Google Chrome相比,Mozilla Firefox中的JavaScript排序功能输出有所不同 - JavaScript Sort function Output is different in Mozilla Firefox Compared to Google Chrome 如何在Chrome和Firefox中将此字符串转换为Javascript日期? - How can I convert this string to a Javascript date in both Chrome and Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM