Time conversion in javascript policy

Hi everyone,

I tried to get epoch time in javascript, and here is what I did:

var begin_date = context.getVariable("date_begin"); //20171001

var begin_year = begin_date[0] + begin_date[1] + begin_date[2] + begin_date[3];
var begin_month = begin_date[4] + begin_date[5];
var begin_day = begin_date[6] + begin_date[7];


var raw_begin_date = begin_month + '/' + begin_day + '/' + begin_year + " 00:00:00";
var converted_begin_date = new Date(raw_begin_date);
converted_begin_date = converted_begin_date.getTime();

context.setVariable("date_begin", converted_begin_date);
<br>

Using the code above, I ran it on browser's console and my node.js and I get is 1506790800000. But in edge, what I got is 1.506816E12

What should I do to fix this?

Regards,

Yoga

Solved Solved
1 6 1,607
2 ACCEPTED SOLUTIONS

HI @Yoga Prakoso

While assigning it to a flow variable, convert it to a String

context.setVariable("date_begin", converted_begin_date.toString());

That could fix the issue. Please try and let us know

View solution in original post

Not applicable

What calls my attention is that epocs are not equal.

Browser: 1506790800000

GMT: Saturday, 30th September de 2017 17:00:00

Not the value we wanted

Edge: 1506816000000 (equals 1.506816E12)

GMT: Sunday, 1st October de 2017 0:00:00

The right value!

In addition to @Sai Saran Vaidyanathan's answer, I suggest you to include timezone on your

raw_begin_date

View solution in original post

6 REPLIES 6

HI @Yoga Prakoso

While assigning it to a flow variable, convert it to a String

context.setVariable("date_begin", converted_begin_date.toString());

That could fix the issue. Please try and let us know

Thank you @Sai Saran Vaidyanathan this really fix the issue.

Not applicable

What calls my attention is that epocs are not equal.

Browser: 1506790800000

GMT: Saturday, 30th September de 2017 17:00:00

Not the value we wanted

Edge: 1506816000000 (equals 1.506816E12)

GMT: Sunday, 1st October de 2017 0:00:00

The right value!

In addition to @Sai Saran Vaidyanathan's answer, I suggest you to include timezone on your

raw_begin_date

Good point!

Except "the right value" is not quite true. Both 1506790800000 and 1506816000000 are "correct".

The reason there may be different correct values obtained in the browser's console versus Apigee Edge: the browser may be running in one timezone, while the message-processor for Apigee Edge is running "in the cloud", presumably in a different timezone. They have different timezone offsets.

So the result is as expected. "20171001" means one time in the browser, and means a different moment in time, in the Apigee Edge cloud.

The solution to this is as Mauricio suggests: to include the timezone in the raw_begin_date. As an alternative, you may "presume" UTC everywhere. But the original conversion code does not do that now.

You can use the jjs REPL tool that is included with the JDK to test this. It interprets Javascript from your terminal. Here's what I learned on my workstation:

$ jjs
jjs> var dstr = '2017/10/01 00:00:00 GMT'
jjs> var d = Date.parse(dstr);
jjs> d 
1506816000000
jjs> dstr = '2017/10/01 00:00:00';
2017/10/01 00:00:00
jjs> d = Date.parse(dstr);
1506841200000
jjs> var diffMilliseconds = Date.parse(dstr) - 1506816000000;
jjs> var hoursFromUtc = (diff/3600)/1000;
jjs> hoursFromUtc
7
jjs> exit(1)


Note that the delta from UTC varies! Doing the same calculation with today's date, 2017/11/07, the delta is different.

$ jjs
jjs> var dstr = '2017/11/07 00:00:00 GMT';
jjs> var d = Date.parse(dstr);
jjs> d
1510012800000
jjs> dstr = '2017/11/07 00:00:00'; 
2017/11/07 00:00:00
jjs> var d2 = Date.parse(dstr);
jjs> var diff = d2 - d;
jjs> var hoursFromUtc = (diff/3600)/1000;
jjs> hoursFromUtc
8
jjs> 

This is because Daylight savings time (DST) in my locale just ended, a few days ago, whereas DST was in effect on 2017/10/01 .

Thank you @Dino

Different timezone applied in the system already in my mind before I made this post but I didn't mention it, however it may help future readers when they get the same issue.

Thank you @Mauricio Navarro Miranda for showing this matter.

I actually realized it before I posted this question but didn't mention it.

However it may help future readers who read this post.