Help writing a simple JS script
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}"/>
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@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}"/>
JavaScript timestamps are expressed in milliseconds, and the timestamps from freeswitch are in nanoseconds, so you need to convert the timestamp to ms before passing it to Date: var date = new Date(argv / 1000); -Chad From: freeswitch-users-bounces@lists.freeswitch.org [mailto:freeswitch-users-bounces@lists.freeswitch.org] On Behalf Of George Diamantopoulos Sent: Wednesday, October 24, 2012 11:49 PM To: freeswitch-users@lists.freeswitch.org Subject: [Freeswitch-users] Help writing a simple JS script 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}"/>
participants (2)
-
Chad Engler
-
George Diamantopoulos