On Jan 24, 4:54 am, Matt Doran <[email protected]> wrote:
> Hi there,
>
> I'm embedded Rhino into our application (for end user scripting), and
> I'd like to expose some dates into Javascript so that it feels as
> native as possible (i.e. they are JS date objects, not
> java.util.Date).
>
> Rhino doesn't seem convert the Java date type into the native JS
> type. So only the java date methods are available to the user.
>
> I'm happy to manually instantiate a "native" Javascript object, but
> how do I do this from Java? The NativeDate type is package protected,
> and I can't see any obvious way to do this.
>From Java to JS, with date being a java.util.Date and cx being a
org.mozilla.javascript.Context:
Object[] args = { new Long(date.getTime()) };
cx.newObject(scope, "Date", args);
And from JS to Java, with s as org.mozilla.javascript.Scriptable:
if ("Date".equals(s.getClassName())
date = new Date((long) ScriptRuntime.toNumber(s));
I agree there should be an easy way to do this.
Hannes
> Thanks in advance,
> Matt
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino