I am trying to use moment js library in my GWT JSNI code:
public native String momentNow(String date) /*-{
$wnd.console.log("Date: " + date);
return $wnd.moment("YYYY-MM-DD HH:mm Z", date).format();
}-*/;
With a example input: 2014-02-04 07:47 +0800
But, what the method returns is "Invalid date" String. What could be wrong in my code?
Hmm... this is odd:
This works:
moment("YYYY-MM-DD HH:mm ZZ", moment().format("YYYY-MM-DD HH:mm ZZ"))
moment("YYYY-MM-DD HH:mm ZZ", "2014-02-04 07:47 +0800")
+moment("YYYY-MM-DD HH:mm ZZ", "2014-02-04 07:47 +0800")
But this doesn't work:
moment(
"YYYY-MM-DD HH:mm ZZ",
moment().format("YYYY-MM-DD HH:mm ZZ")
).format()
And this works:
moment( +moment("YYYY-MM-DD HH:mm ZZ", "2014-02-04 07:47 +0800") ).format()
So is that a workaround?
You have the parameters in the wrong order. Per the documentation , the format string should be passed as the second parameter.
moment(date, "YYYY-MM-DD HH:mm Z").format()
However, since this format is one of the default ISO strings listed here , you can just omit it.
moment(date).format()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.