Skip to content

Commit 609d457

Browse files
committed
binding for Date.prototype.setMonth()
1 parent a81827c commit 609d457

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/js.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,12 @@ extern "C" {
10541054
#[wasm_bindgen(method, js_name = setMinutes)]
10551055
pub fn set_minutes(this: &Date, minutes: u32) -> f64;
10561056

1057+
/// The setMonth() method sets the month for a specified date according to the currently set year.
1058+
///
1059+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth
1060+
#[wasm_bindgen(method, js_name = setMonth)]
1061+
pub fn set_month(this: &Date, month: u32) -> f64;
1062+
10571063
/// The toDateString() method returns the date portion of a Date object
10581064
/// in human readable form in American English.
10591065
///

tests/all/js_globals/Date.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,45 @@ fn set_minutes() {
919919
.test()
920920
}
921921

922+
#[test]
923+
fn set_month() {
924+
project()
925+
.file(
926+
"src/lib.rs",
927+
r#"
928+
#![feature(proc_macro, wasm_custom_section)]
929+
930+
extern crate wasm_bindgen;
931+
use wasm_bindgen::prelude::*;
932+
use wasm_bindgen::js::Date;
933+
934+
#[wasm_bindgen]
935+
pub fn set_month(this: &Date, month: u32) -> f64 {
936+
this.set_month(month)
937+
}
938+
"#,
939+
)
940+
.file(
941+
"test.js",
942+
r#"
943+
import * as assert from "assert";
944+
import * as wasm from "./out";
945+
946+
export function test() {
947+
let event1 = new Date('August 19, 1975 23:15:30');
948+
let event2 = new Date('April 19, 1975 23:15:30');
949+
950+
let eventMsFromUnixEpoch = wasm.set_month(event1, 3);
951+
952+
assert.equal(eventMsFromUnixEpoch, 167148930000);
953+
assert.equal(event1.getTime(), event2.valueOf());
954+
assert.equal(event1.getMonth(), 3);
955+
}
956+
"#,
957+
)
958+
.test()
959+
}
960+
922961
#[test]
923962
fn to_date_string() {
924963
project()

0 commit comments

Comments
 (0)