diff --git a/src/js.rs b/src/js.rs index 47a36cc5..287e217d 100644 --- a/src/js.rs +++ b/src/js.rs @@ -376,6 +376,12 @@ extern { #[wasm_bindgen] extern { pub type Date; + + /// The toJSON() method returns a string representation of the Date object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON + #[wasm_bindgen(method, js_name = toJSON)] + pub fn to_json(this: &Date) -> JsString; /// The toLocaleDateString() method returns a string with a language sensitive /// representation of the date portion of this date. The new locales and options diff --git a/tests/all/js_globals/Date.rs b/tests/all/js_globals/Date.rs index 7166175d..06994a25 100644 --- a/tests/all/js_globals/Date.rs +++ b/tests/all/js_globals/Date.rs @@ -2,6 +2,34 @@ use super::project; +#[test] +fn to_json() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::{Date, JsString}; + + #[wasm_bindgen] + pub fn to_json(this: &Date) -> JsString { + this.to_json() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let date = new Date('August 19, 1975 23:15:30 UTC'); + + assert.equal(wasm.to_json(date), '1975-08-19T23:15:30.000Z'); + } + "#) + .test() +} + #[test] fn to_locale_date_string() { project()