How does JS date comparison work?

前端 未结 3 1123
悲&欢浪女
悲&欢浪女 2020-12-17 00:45

Let\'s assume I have a proper Date object constructed from the string: \"Tue Jan 12 21:33:28 +0000 2010\".

var dateString = \"Tue          


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 01:43

    Relational operations on objects in ECMAScript rely on the internal ToPrimitive function (with hint number) that you can access, when it is defined, using valueOf.

    Try

    var val = new Date().valueOf();
    

    You'll get the internal value of the date which is, as in many languages, the number of milliseconds since midnight Jan 1, 1970 UTC (the same that you would get using getTime()).

    This means that you're, by design, ensured to always have the date comparison correctly working.

    This article will give you more details about toPrimitive (but nothing relative to comparison).

提交回复
热议问题