[Freeswitch-users] Help writing a simple JS script

George Diamantopoulos georgediam at gmail.com
Thu Oct 25 08:52:50 MSD 2012


I think I did it after all. It turns out taking a break for 30 minutes can
help when (psydo)coding. Sorry for polluting this list and here's the code
that did it:

<action application="javascript" data="unixtocal.js ${created_time}"/>

var date = new Date(argv/1000);
var hours = date.getHours();
hours = hours.toString();
var minutes = date.getMinutes();
minutes = minutes.toString();
var seconds = date.getSeconds();
seconds = seconds.toString();
var ms = date.getMilliseconds();
ms = ms.toString();
var year = date.getFullYear();
year = year.toString();
var day = date.getDate();
day = day.toString();
var month = date.getMonth() +1;
month = month.toString();

the rest was OK

On Thu, Oct 25, 2012 at 6:48 AM, George Diamantopoulos <georgediam at gmail.com
> wrote:

> Hello,
>
> I've been trying to write a simple JS script, which is called from within
> the XML dialplan, in order to convert the value of ${created_time} to
> something more meaningful than UNIX time in ms. The problem is when I try
> to print something to the console (see log application below), I get NaN
> (not a number) for variable ${created_date}, even though I don't want a
> number (a string is fine, I tried enforcing this with toString()).
> Moreover, you'll notice I've left out var month = date.getMonth();, because
> if I don't I get the following error: TypeError: date.getMonth is not a
> function. I can't figure out why, it's documented in JS in the date object,
> and the syntax is identical to other functions I use to acquire object
> values. Any hints would be greatly appreciated, I've been trying different
> code variations for over two hours now and I'm stuck. Thanks.
>
> Here's the call in the dialplan:
>
> <action application="javascript" data="unixtocal.js $created_time"/>
>
> And here's the JS script itself:
>
> var date = new Date(argv);
> var hours = date.getHours();
> hours = hours.toString();
> var minutes = date.getMinutes();
> minutes = minutes.toString();
> var seconds = date.getSeconds();
> seconds = seconds.toString();
> var ms = date.getMilliseconds();
> ms = ms.toString();
> var year = date.getFullYear();
> year = year.toString();
> var day = date.getDate();
> day = day.toString();
>
> session.execute("set", "created_year="+year);
> session.execute("set", "created_date="+day);
> session.execute("set", "created_hours="+hours);
> session.execute("set", "created_minutes="+minutes);
> session.execute("set", "created_seconds="+seconds);
> session.execute("set", "created_ms="+ms);
>
> Then the dialplan takes over again, for example:
>
> <action application="log" data="INFO Date is ${created_date}"/>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20121025/33207f20/attachment.html 


Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users mailing list