2017-09-23 7 views
0

Math.max() 함수를 사용하여 여러 Date 개체에서 최대 날짜를 계산할 수있는 이유를 100 % 확신 할 수 없습니다.Math.max()를 사용하여 최대 JavaScript Date Object 설명서를

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 PhpStorm는 나에게 다음과 같은 오류를주는 유지 사양을 사용하여 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.

모든 최신 브라우저에서이 방법을 테스트했지만 오류가 발생하지 않았습니다. 비록 이것이 이전 브라우저와 호환되는지 궁금합니다.

왜은 Date 개체를 전달하여 작동합니까?

+1

때문에 강제 형 변환, 수학의 번호는 숫자입니다. 그러면 날짜를 숫자로 변환하는 것입니다. 그래서 PHPStorm은 틀렸고 Intellisense 인 것처럼 보이며 말도 안되는 소리입니다. – Keith

+0

나는 왜 당신이 "* ... 그 사양이 명확하지 않다고 말하는지 모르겠다. * 그들 중 아무도 그렇게 말하지 않을 때.1을 제외하고, "* ... 각 인수에 ToNumber를 호출합니다. *"(이것은 ed 1이 그렇게 말하지 않은 것입니다). [* ToNumber *] (http://ecma-international.org/ecma-262/8.0/#sec-tonumber)가 날짜를 처리하는 방법을 보려면 해당 링크를 따르십시오. 짧은 대답은 내부 * [time value *]를 반환하는 * valueOf *를 호출한다는 것입니다 (http://ecma-international.org/ecma-262/8.0/#sec-time-values-and-time-range), 이는 숫자입니다 (그리고 * NaN', BTW 일 수도 있습니다). – RobG

+0

@RobG 설명해 주셔서 감사합니다. 나는 그것을 지금 본다. 이 질문을 할 당시, 나는 '날짜'객체가 NaN이라는 인상을 받았다. 분명히, 나는 틀렸다. – Daerik

답변

2

유효한 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({}));

+1

또한 이것을 위해'date.valueOf()'를 사용할 수 있습니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf –

+0

@DuncanThacker 당신 말이 맞아. –

+1

짧은 +'날짜입니다. – RobG

0

NaN 특별한이다 "숫자가 아닌 모든 것"이 아닌 숫자 유형의 값. Math.sqrt(-1)과 같은 번호를 반환해야하는 함수에 사용됩니다.

또한 함수가 숫자를 예상하면 JavaScript는 사용자가 제공 한 번호를 만드는 것이 가장 좋습니다. Date에는 숫자 표현 (Date.valueOf을 사용하여 얻을 수 있음)이 있기 때문에이를 사용합니다.

은 (옆 흥미로운으로, 당신은 값이 직접 false를 반환합니다 NaN로를 isNaN 기능을 사용하지만, 비교하여 NaN 있는지 여부를 테스트 할 수 있습니다. 그 중 하나만 자바 스크립트 일)