Skip to content

js_sys::Date::set_date() does not accept negative days #4500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eroman-code opened this issue May 2, 2025 · 0 comments
Open

js_sys::Date::set_date() does not accept negative days #4500

eroman-code opened this issue May 2, 2025 · 0 comments
Labels

Comments

@eroman-code
Copy link

Describe the Bug

js_sys::Date::set_date() currently defines its day argument as an unsigned type:

pub fn set_date(this: &Date, day: u32) -> f64;

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:

const theBigDay7 = new Date(theBigDay).setDate(-50); // 1962-05-11 (11th of May 1962)

Steps to Reproduce

  1. js_sys::Date::new_0().set_date(-50);
  2. 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.

@eroman-code eroman-code added the bug label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant