I have the following code:
var date = new Date(2010,09,09); //09.10.2010 00:00
date.setHours(-1); //sets the date to 08.10.2010 23:00
Calling date.setHours(-1); sets the date to 23:00 at the previous day (at least for Opera and Chrome).
Is this legal?
Not only is it legal, is it required.
The behaviour is officially defined in the ECMAScript specification, section 15.9.5.34:
Date.prototype.setHours (hour [, min [, sec [, ms ] ] ] )
If min is not specified, this behaves as if min were specified with the value
getMinutes().If sec is not specified, this behaves as if sec were specified with the value
getSeconds().If ms is not specified, this behaves as if ms were specified with the value
getMilliseconds().
- Let t be the result of LocalTime(this time value).
- Let h be ToNumber(hour).
- If min is not specified, then let m be MinFromTime(t); otherwise, let m be ToNumber(min).
- If sec is not specified, then let s be SecFromTime(t); otherwise, let s be ToNumber(sec).
- If ms is not specified, then let milli be msFromTime(t); otherwise, let milli be ToNumber(ms).
- Let date be MakeDate(Day(t), MakeTime(h, m, s, milli)).
- Let u be TimeClip(UTC(date)).
- Set the [[PrimitiveValue]] internal property of this Date object to u.
- Return u
And the specification for MakeTime, used in step 6, ultimately calculates a millisecond offset by multiplying the various parts together. There are no preconditions for bounds on the arguments (other than that they are finite), so a negative number of hours is legal, and will result in a negative result.
Thus the overall result, that is midnight + (-1 hours), does need to be 23:00 on the previous day to conform with the spec.
Yes, this is effectively what the specification dictates, so it would be an "illegal" implementation if it didn't.
The actual computation happens in MakeDate. The date is converted into milliseconds, as is the -1 hours. These are then added. Adding a negative number does subtraction, so you get an earlier date.
If
dayis not finite ortimeis not finite, returnNaN.Return
day×msPerDay+time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With