Following script does what you need;
<script language="js">
function pad(number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
}
now = new Date();
isoString = now.getUTCFullYear()
+ '-' + pad( now.getUTCMonth() + 1 )
+ '-' + pad( now.getUTCDate() )
+ 'T' + pad( now.getUTCHours() )
+ ':' + pad( now.getUTCMinutes() )
+ ':' + pad( now.getUTCSeconds() )
+ '.' + String( (now.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5
)
+ 'Z';
print(isoString);
</script>
This is based on the answer from [1].
[1] https://community.oracle.com/thread/2607436?start=0&tstart=0
Thanks,
Raj.
On Tue, Mar 29, 2016 at 5:16 PM, Lakmini Chathurika <[email protected]>
wrote:
> Hi Nirodha
>
> Thanks for the quick response. I'll try using it.
>
> Thanks & Regards
> Lakmini.
>
>
> On Tue, Mar 29, 2016 at 9:36 PM, Nirodha Gallage <[email protected]> wrote:
>
>> Hi Lakmini,
>>
>> Javascript library comes with WSO2 ESB does not support all the
>> functions, only a limited set of basic functions are supported. That's what
>> causing this error. But I think you can use some of the functions as
>> mentioned in [1] to read elements of the date and then construct the date
>> as the way you want.
>>
>> [1] http://javascript.info/tutorial/datetime-functions
>>
>> Thanks
>>
>> On Tue, Mar 29, 2016 at 9:15 PM, Lakmini Chathurika <[email protected]>
>> wrote:
>>
>>> Hi All,
>>>
>>> I'm trying to convert a javaScript date (Eg: Tue Mar 29 2016 20:48:38
>>> GMT+0530 (IST)) in to ISO date format (Eg:2016-03-29T15:21:09.701Z) using
>>> ESB Script mediator. For the conversion I used "toISOString()" [1] method.
>>> But I got the following error.
>>>
>>> [2016-03-29 20:38:56,681] ERROR - ScriptMediator The script engine
>>> returned an error executing the inlined js script function mediate
>>> com.sun.phobos.script.util.ExtendedScriptException:
>>> org.mozilla.javascript.EcmaError: TypeError: Cannot find function
>>> toISOString. (<Unknown Source>#2) in <Unknown Source> at line number 2
>>> at
>>> com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
>>> at javax.script.CompiledScript.eval(CompiledScript.java:92)
>>> at
>>> org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:300)
>>> at
>>> org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:239)
>>> at
>>> org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:207)
>>> at
>>> org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
>>> at
>>> org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
>>> at
>>> org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:149)
>>> at
>>> org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:185)
>>> at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
>>> at
>>> org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:395)
>>> at
>>> org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:142)
>>> at
>>> org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
>>> at
>>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>>> at
>>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>>> at java.lang.Thread.run(Thread.java:745)
>>> Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find
>>> function toISOString. (<Unknown Source>#2)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3368)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3428)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2052)
>>> at
>>> org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:117)
>>> at org.mozilla.javascript.gen.c6028._c0(<Unknown Source>:2)
>>> at org.mozilla.javascript.gen.c6028.call(<Unknown Source>)
>>> at
>>> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
>>> at
>>> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
>>> at org.mozilla.javascript.gen.c6028.call(<Unknown Source>)
>>> at org.mozilla.javascript.gen.c6028.exec(<Unknown Source>)
>>> at
>>> com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
>>> ... 15 more
>>>
>>> Proxy:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <proxy xmlns="http://ws.apache.org/ns/synapse"
>>> name="testproxy"
>>> transports="https,http"
>>> statistics="disable"
>>> trace="disable"
>>> startOnLoad="true">
>>> <target>
>>> <inSequence>
>>> <script language="js">var d=new Date();
>>> var isoFormat = d.toISOString();
>>> print(isoFormat);</script>
>>> </inSequence>
>>> </target>
>>> <description/>
>>> </proxy>
>>>
>>> Are there any limitations in ESB Script Mediator or am I doing something
>>> wrong here?
>>> Any help is greatly appreciated.
>>>
>>> [1].http://www.w3schools.com/jsref/jsref_toisostring.asp
>>>
>>> Thanks & Regards
>>> Lakmini.
>>>
>>
>>
>>
>> --
>>
>> *Nirodha Gallage*
>> Associate Technical Lead, QA.
>> WSO2 Inc.: http://wso2.com/
>> Mobile: +94716429078
>>
>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
Rajkumar Rajaratnam
Committer & PMC Member, Apache Stratos
Software Engineer, WSO2
Mobile : +94777568639
Blogs : wso2tech.blogspot.com, wso2tech.wordpress.com
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev