You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In fact, the MDN reference for setDate explicitly includes an example of using setDate() with a negative date, clarifying this behavior:
const theBigDay7 = new Date(theBigDay).setDate(-50); // 1962-05-11 (11th of May 1962)
Steps to Reproduce
js_sys::Date::new_0().set_date(-50);
Compile error: cannot apply unary operator - to type u32 unsigned values cannot be negated
Expected Behavior
Some way to call setDate() with a negative number of days.
I don't know enough about js-sys to be opinionated on the solution. Conceptually Date::set_date() could be changed to take an day: i32, although that would be a breaking change. Other alternatives could be use a generic parameter which accepts a variety of integer types, or introduce a one-off extra method for setting signed dates.
Additional Context
The pattern of setting a negative date is used in some Javascript applications for doing date calculations, to find a date that is some number of days before another one. For (example here it is described in stackoverflow).
Currently this pattern cannot be directly translated to js-sys, since its setDate expects an unsigned offset. There are alternative approaches to calculating such dates, however this would be a nice thing to support directly in the bindings.
The text was updated successfully, but these errors were encountered:
Describe the Bug
js_sys::Date::set_date()
currently defines itsday
argument as an unsigned type:This definition is less flexible than the ECMAScript spec for setDate() which allows for negative day offsets.
In fact, the MDN reference for setDate explicitly includes an example of using
setDate()
with a negative date, clarifying this behavior:Steps to Reproduce
js_sys::Date::new_0().set_date(-50);
cannot apply unary operator - to type u32 unsigned values cannot be negated
Expected Behavior
Some way to call
setDate()
with a negative number of days.I don't know enough about js-sys to be opinionated on the solution. Conceptually
Date::set_date()
could be changed to take anday: i32
, although that would be a breaking change. Other alternatives could be use a generic parameter which accepts a variety of integer types, or introduce a one-off extra method for setting signed dates.Additional Context
The pattern of setting a negative date is used in some Javascript applications for doing date calculations, to find a date that is some number of days before another one. For (example here it is described in stackoverflow).
Currently this pattern cannot be directly translated to js-sys, since its
setDate
expects an unsigned offset. There are alternative approaches to calculating such dates, however this would be a nice thing to support directly in the bindings.The text was updated successfully, but these errors were encountered: