javascript - 使用 Math.max() 计算最大日期 JavaScript 日期对象文档

标签 javascript date max

我不是 100% 确定为什么您能够从多个 Date 计算最大日期使用 Math.max() 的对象功能。我的 IDE PhpStorm 不断出现以下错误:

Argument type Date is not assignable to parameter type number

它可以分配给参数,如我的代码片段所示:

/* Variable Defaults */
var dateOne = new Date();
var dateTwo = new Date('2017-01-21');
var dateThree = new Date('11/16/2016');
var dateMax = Math.max(dateOne, dateTwo, dateThree);

/* Console Data */
console.log('dateOne', dateOne);
console.log('dateTwo', dateTwo);
console.log('dateThree', dateThree);
console.log('dateMax', dateMax + ': ' + new Date(dateMax));

我决定研究一下规范,看看我的 IDE 是否使用了较旧的标准,但我的研究并没有实现我的愿望,即教育自己为什么这种方法应该首先起作用:

ECMAScript 1st Edition (ECMA-262)

15.8.2.11 max(x, y)

  • Returns the larger of the two arguments.
  • If either argument is NaN, the result is NaN.
  • If x>y, the result is x.
  • If y>x, the result is y.
  • If x is +0 and y is +0, the result is +0.
  • If x is +0 and y is -0, the result is +0.
  • If x is -0 and y is +0, the result is +0.
  • If x is -0 and y is -0, the result is -0.

ECMAScript 5.1 (ECMA-262)

15.8.2.11 max ( [ value1 [ , value2 [ , … ] ] ] )

  • Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.
  • If no arguments are given, the result is −∞.
  • If any value is NaN, the result is NaN.
  • The comparison of values to determine the largest value is done as in 11.8.5 except that +0 is considered to be larger than −0.
  • The length property of the max method is 2.

ECMAScript 2015 (6th Edition, ECMA-262)

20.2.2.24 Math.max ( value1, value2 , …values )

  • Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.
  • If no arguments are given, the result is −∞.
  • If any value is NaN, the result is NaN.
  • The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0.
  • The length property of the max method is 2.

ECMAScript Latest Draft (ECMA-262)

20.2.2.24 Math.max ( value1, value2, ...values )

  • Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.
  • If no arguments are given, the result is -∞.
  • If any value is NaN, the result is NaN.
  • The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.

我已经在所有现代浏览器中测试了此方法,并且没有生成任何错误。虽然我确实想知道这是否与旧版浏览器兼容。

为什么 Math.max() 可以通过传入 Date 对象来工作,而规范明确指出不应该这样做?

最佳答案

由于有效的 Date() 不是 NaN,因此可以使用 Number() 函数将其转换为数字,+ 一元运算符或 valueOf() 函数:

var date = new Date();
console.log(isNaN(date) + ',' + Number(date));
console.log(isNaN(date) + ',' + +date);
console.log(isNaN(date) + ',' + date.valueOf());
console.log(isNaN(2) + ',' + Number(2));
console.log(isNaN('2') + ',' + Number('2'));
console.log(isNaN('xx') + ',' + Number('xx'));
console.log(isNaN(['a']) + ',' + Number(['a']));
console.log(isNaN({}) + ',' + Number({}));

关于javascript - 使用 Math.max() 计算最大日期 JavaScript 日期对象文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46384462/

相关文章:

Java - 计算除节假日(周六和周日)之外的天数

java - 字符串到java中的日期时间

mysql - 使用 group by 仅更新具有最大值的记录

javascript - 如何使用 csv 文件创建条形图

javascript - 相关单选按钮和下拉列表

javascript - 如何使用 Protractor 获取当前 url?

javascript - 展开输入日期日历(浏览器原生)

r - 如何在两个向量之间取成对并行最大值?

mysql - MAX() 函数未按预期工作

javascript - 当字符串通过javascript中的encodeURIComponent通信时,将字符串解析为原始形式