From dd3e8cc8b875cd742e190954fcbf8fe4c01eb6f2 Mon Sep 17 00:00:00 2001 From: Sendil Kumar Date: Mon, 25 Jun 2018 10:16:58 +0200 Subject: [PATCH] Add date to iso string --- src/js.rs | 11 ++++++++++- tests/all/js_globals/Date.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 287e217d..24df082e 100644 --- a/src/js.rs +++ b/src/js.rs @@ -376,7 +376,16 @@ extern { #[wasm_bindgen] extern { pub type Date; - + + /// The toISOString() method returns a string in simplified extended ISO format (ISO + /// 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or + /// ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, + /// as denoted by the suffix "Z" + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString + #[wasm_bindgen(method, js_name = toISOString)] + pub fn to_iso_string(this: &Date) -> JsString; + /// 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 diff --git a/tests/all/js_globals/Date.rs b/tests/all/js_globals/Date.rs index 06994a25..601a04ca 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_iso_string() { + 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_iso_string(this: &Date) -> JsString { + this.to_iso_string() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let date = new Date('05 October 2011 14:48 UTC'); + + assert.equal(wasm.to_iso_string(date), '2011-10-05T14:48:00.000Z'); + } + "#) + .test() +} + #[test] fn to_json() { project()