Closed
Bug 368593
Opened 18 years ago
Closed 10 years ago
date.setDate to negative number works... almost
Categories
(Core :: JavaScript Engine, defect)
Core
JavaScript Engine
Tracking
()
RESOLVED
WORKSFORME
People
(Reporter: dbialac, Unassigned)
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1
Build Identifier: 2.0.0.1
When using the Date object, setting to a 0 or negative date works for the most part, except in February. In the case I found, this is the behavior:
var dt = new Date;
dt.setMonth(1);
dt.setDate( -2 );
alert (dt.Month() + '/' + dt.getDay()); // Returns '0/28', this is correct
dt.setMonth(1);
dt.setDate( -1 );
alert (dt.Month() + '/' + dt.getDay()); // Returns '1/27', this is incorrect!
In all other months, everything works fine. Just not February.
Reproducible: Always
Steps to Reproduce:
1.See details
2.
3.
Actual Results:
Got date of '1/27'
Expected Results:
'0/30'
Comment 1•18 years ago
|
||
Basically the same problem as bug 288495, use setMonth(N + 1, -1) if you want to set the date to the penultimate day of month N.
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
Reporter | ||
Updated•18 years ago
|
Status: RESOLVED → UNCONFIRMED
Resolution: DUPLICATE → ---
Reporter | ||
Comment 2•18 years ago
|
||
Although related in that they handle the date object, this bug has a difference which makes it not 'invalid'. From bug 288495 (Brendan Eich):
INVALID. BTW, this worksforme on Linux, so it's not a Windows bug, and I don't
see why it wouldn't reproduce in Firefox or Camino or the Mozilla Suite on the
Mac too. ECMA-262 specifies exactly what happens here. Consider the commented
code broken up into one statement per line:
var d=new Date(); // d is a date on March 31, 2005.
d.setYear(2005); // no-op: year doesn't change, and date is still 31.
d.setMonth(8); // 8 is September, but "30 days hath September,
// April, June, and November", so the date 31 does
// in fact overflow, and it must be wrapped to 1,
// with the carry out advancing the month to 9,
// for September 1, 2005.
d.setDate(1); // no-op: date is already 1.
alert(d.toString());
In the case I'm pointing out we're starting from February of 2007 and "Overflowing" back to January 2007. According to the above, when setDate() is passed -1, it should return as January 30, 2007, but it doesn't -- it returns as February 27, 2007. When passed all other negative and zero numbers, the correct behavior is exhibited. Ex: passing -2 returns January 29, 2007. This is a legitimate bug!
Comment 3•18 years ago
|
||
Are negative values even allowed in date.setDate()? Nothing in the ECMAScript 3rd edition spec concerning this says anything about negative values. I'd say this bug is invalid.
If it's not invalid, then passing negative values is indeed buggy. Try doing dt.setDate(-1) multiple times - each time, it goes back a month. On another note, if you created a new Date object before doing setDate(-x) again, your example would work as expected.
Reporter | ||
Comment 4•18 years ago
|
||
I'm not certain about anything specifically mentioned about negative values, only the behavior of overflowing, so the question becomes is using a negative number overflowing? If you go in the positive direction, this is always the case so shouldn't it also be the case for the negative direction?
As for doing dt.setDate(-1) multiple times, it should go back a month each time since you are working from a relative point of the month that dt is currently set to.
However, I want to point out that this bug is for a _very_ specific example. Only when starting with February and only when passing -1 does this bug occur (at least in 2007). I would suspect that this has to do with some sort of special handling for the end of the month of February. In any other month passing -1 (or any other negative value for that matter) works as expected.
Comment 5•18 years ago
|
||
IIRC, just create another Date object to prevent the overflow "bug":
var dt = new Date;
dt.setMonth(1);
dt.setDate( -2 );
dt = new Date;
dt.setMonth(1);
dt.setDate( -1 );
Assignee | ||
Updated•11 years ago
|
Assignee: general → nobody
Comment 6•10 years ago
|
||
No longer reproducible - Resolving as WFM.
Tested with:
---
dt = new Date("2007-01-29T12:00:00Z");
dt.setMonth(1);
dt.setDate(-1);
assertEq(`${dt.getMonth()} - ${dt.getDate()}`, "1 - 27");
dt = new Date("2007-01-29T12:00:00Z");
dt.setMonth(1);
dt.setDate(-2);
assertEq(`${dt.getMonth()} - ${dt.getDate()}`, "1 - 26");
---
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago → 10 years ago
Resolution: --- → WORKSFORME
You need to log in
before you can comment on or make changes to this bug.
Description
•