Closed Bug 1318590 Opened 8 years ago Closed 8 years ago

Date() different results than new Date()

Categories

(Core :: JavaScript Engine, defect)

50 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: jmichae3, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 5.1; rv:50.0) Gecko/20100101 Firefox/50.0
Build ID: 20161104212021

Steps to reproduce:

var d=Date(2016,11,15);
undefined
d.valueOf()
"Thu Nov 17 2016 21:45:32 GMT-0800 (Pacific Standard Time)"
var d = new Date(2016,11,15);
undefined
d.valueOf();
1481788800000
var d=new Date(2016,11,15,0,0,0,0),d2=d+20;
undefined
d2.getFullYear()
TypeError: d2.getFullYear is not a function [Learn More]
var d=new Date(2016,11,15,0,0,0,0)+20
undefined
d.getFullYear()
TypeError: d.getFullYear is not a function [Learn More]


Actual results:

see above. normally I can add an integer to a Date object to add days, or subtract days, etc. but this does not work. also, .valueOf() between Date(2016,11,15) and new Date(2016,11,15) returns either a string or an integer. why the string? there are already string output functions in the Date object API. being able to add or subtract 20 days (or .getDay() for a table beginning on a calendar) is far more useful than a string to parse, where adding 2000 simply appends it to the string.

on opera, I get
d+=2000;
"Fri Feb 01 1901 00:00:00 GMT-0800 (Pacific Standard Time)2000"
and similar results to ff. 



Expected results:

after d+=2000; I should have been able to 
d.getFullYear() (d is now a string and this function is gone)
d.getMonth()
d.getDate()
d.getDay()
d.getHour()
d.getMinute()
d.getSecond()
d.getMillisecond()
(sp? some functions end with s and some don't - ugh)

if people are crying out for string functionality, give it to them via the API, there is already UTC string etc and you can also build your own. why not provide an SQL-format datetime?
Component: Untriaged → JavaScript Engine
Product: Firefox → Core
It's not an implementation bug.
Also changing the behavior will break the web.


(In reply to Jim Michaels from comment #0)
> see above. normally I can add an integer to a Date object to add days, or
> subtract days, etc. but this does not work.

https://tc39.github.io/ecma262/#sec-addition-operator-plus-runtime-semantics-evaluation
https://tc39.github.io/ecma262/#sec-subtraction-operator-minus-runtime-semantics-evaluation

About '+' operator.
If both operands are number, it adds them in term on number.
Otherwise, it converts them to string and concatenates.

About '-' operator.
It converts operands to number and substracts.


> also, .valueOf() between
> Date(2016,11,15) and new Date(2016,11,15) returns either a string or an
> integer. why the string?

https://tc39.github.io/ecma262/#sec-date-year-month-date-hours-minutes-seconds-ms

If Date is called without "new", it returns string representation of current time.
if Date is called with "new", it creates Date instance with given date.
so the returned value is totally different.
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.