Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18643

Re: Why is this returning NaN

Message-ID <1542531.g9YUNRc4XZ@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-02-26 18:07 +0100
Subject Re: Why is this returning NaN
Newsgroups comp.lang.javascript
References <kgi46g$ivc$1@dont-email.me> <timstreater-3655E3.12001626022013@news.individual.net>

Show all headers | View raw


Tim Streater wrote:

>  "Tony" <tony.johansson@inport.com> wrote:
>> var diff = Math.abs(new Date() - "2013/02/26 10:24:00");
>> This should be perfect legal I believe.
> 
> […]
> 'new Date()' returns a reference to a Date object. What leads you to
> believe that you can subtract a string from a reference?

Theoretically it could be possible.  Numerous other implicit type 
conversions are being performed in ECMAScript, even some where primitive 
types are being converted to object types (think about 
String.prototype.replace(), or subtracting references to Date instances 
which eventually subtracts the same values as the return values of the 
getTime() methods of the referred objects, respectively).

However, whether type conversion is possible and whether it should be 
possible are two different things.  In this case, a future ECMAScript 
implementation could specify that subtracting *from* a primitive string in 
the ISO time format defined in the current Specification (ES 5.1) a 
reference to a Date instance would convert the string value into a reference 
to a corresponding Date by passing it to the Date constructor, and then 
proceeding as if there had been two references to Date instances in the 
first place.  This would be unambiguous because of the “-” operation being 
performed in the context of the Date instance reference instead of the 
String value:

  var diffMilliseconds = "2013-02-26T10:24:00" - new Date();

instead of

  var diffMilliseconds = new Date("2013-02-26T10:24:00") - new Date();

Also, one could imagine a form of semantic coding found in e.g. POSIX 
date(1) and MySQL where you would write

  var d2 = d + "2 days";

instead of the not inelegant, but still too complicated

  /* See ES 5.1, §§ 15.9.3.2, 9.1, and 8.12.8(!) */
  var d2 = new Date(d);

  d2.setDate(d2.getDate() + 2);
  d2

One should probably have the ability of (perhaps restricted) operator 
overloading in general, then, as in Python.  This would be especially useful 
with user-defined objects.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why is this returning NaN "Tony" <tony.johansson@inport.com> - 2013-02-26 11:54 +0100
  Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-26 12:09 +0100
  Re: Why is this returning NaN "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-02-26 13:45 +0200
    Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-01 00:45 -0300
      Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-01 01:13 -0300
        Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 12:48 +0100
      Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-01 10:06 +0100
      Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 12:46 +0100
        Re: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-01 14:19 +0000
        Re: Why is this returning NaN "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-03-01 12:01 -0300
          Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 21:03 +0100
            Re: Why is this returning NaN "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-03-01 18:25 -0300
              Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-01 22:35 +0100
                Re: Why is this returning NaN Patricia Shanahan <pats@acm.org> - 2013-03-01 20:31 -0800
                Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-02 12:49 +0100
                Re: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 12:32 -0300
                Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-03 18:03 +0100
                Re: Why is this returning NaN Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-03-03 10:16 -0700
                OT: "javascript" was: Why is this returning NaN Christoph Becker <cmbecker69@gmx.de> - 2013-03-03 20:35 +0100
                Re: OT: "javascript" was: Why is this returning NaN "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-03-03 21:43 +0200
                Re: OT: "javascript" was: Why is this returning NaN Tim Streater <timstreater@greenbee.net> - 2013-03-03 19:45 +0000
                "javascript" (was: OT: "javascript") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-03 21:09 +0100
                Re: "javascript" "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 23:19 -0300
                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-04 11:27 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-04 13:09 +0000
                Re: "javascript" Christoph Becker <cmbecker69@gmx.de> - 2013-03-10 20:50 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 20:29 +0000
                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-10 23:23 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 22:48 +0000
                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-10 23:53 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 23:23 +0000
                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-11 02:57 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-11 08:55 +0000
                Re: "javascript" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-11 17:38 +0100
                Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-12 19:29 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-13 14:36 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-13 15:20 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-14 11:18 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-14 10:43 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-15 07:02 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-15 10:28 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-16 07:17 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-16 12:04 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-17 08:17 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-17 11:13 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-17 23:15 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-17 16:42 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-18 07:47 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-18 15:01 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-19 07:34 +1100
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-19 15:04 +0000
                Re: "javascript" Andrew Poulos <ap_prog@hotmail.com> - 2013-03-20 07:40 +1100
                Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-13 12:03 +0000
                Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-14 21:22 +0000
                Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-15 01:44 +0000
                Re: "javascript" Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-16 19:42 +0000
                Re: "javascript" Mike Duffy <Use.web.form@website_in_sig.com> - 2013-03-17 18:25 +0000
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-17 19:01 +0000
                Re: "javascript" Gene Wirchenko <genew@telus.net> - 2013-03-15 10:55 -0700
                Re: "javascript" John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-13 15:21 +0000
                Re: "javascript" (was: OT: "javascript") Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-04 13:07 +0000
                Re: "javascript" Christoph Becker <cmbecker69@gmx.de> - 2013-03-10 20:34 +0100
                Re: "javascript" Tim Streater <timstreater@greenbee.net> - 2013-03-10 19:39 +0000
                Re: OT: "javascript" was: Why is this returning NaN "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-03 22:41 -0300
                Re: OT: "javascript" was: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-04 09:57 +0100
                Re: OT: "javascript" was: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-04 11:25 +0100
                Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-04 14:52 +0000
                Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-10 01:02 +0100
                Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-10 10:52 +0000
                Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-10 12:44 +0100
                Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-10 17:49 +0000
                Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-20 01:21 +0100
                Re: OT: "javascript" was: Why is this returning NaN John G Harris <niam@jghnorth.org.uk.invalid> - 2013-03-20 15:25 +0000
                Re: OT: "javascript" was: Why is this returning NaN Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-30 01:28 +0100
  Re: Why is this returning NaN Tim Streater <timstreater@greenbee.net> - 2013-02-26 12:00 +0000
    Re: Why is this returning NaN Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-26 18:07 +0100
  Re: Why is this returning NaN Gregor Kofler <usenet@gregorkofler.com> - 2013-02-26 13:42 +0100
  Re: Why is this returning NaN Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-02-27 20:43 +0000
    Re: Why is this returning NaN "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-02-28 09:19 +0100

csiph-web