Mike6979865
Posts: 0
Joined: Fri Jul 11, 2014 3:53 pm

Convert _CreatedAt - Javascript Assistance

Can you please help me with a code to use in a query mapping to a field that is in a list to convert _created_at to Local Date and Time (Pacific Standard Time)? So, I'll map to a field, and then add javascript. Any assistance would be appreciated. Thank you so much.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Convert _CreatedAt - Javascript Assistance

Hello Mike,

Please try this code in mapping:

return (new Date(value.replace(/-/g, '/'))).toUTCString();

Mike6979865
Posts: 0
Joined: Fri Jul 11, 2014 3:53 pm

Convert _CreatedAt - Javascript Assistance

I'm getting "Invalid Date".

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Convert _CreatedAt - Javascript Assistance

Hi Mike,

Your problem is not clear for us..

Is there any change you can provide us more info (screen shot are welcome)?

Also, i've tested given code and here is result:

1 Mapping - http://prntscr.com/4njud6/direct

2 Result - http://prntscr.com/4njunk/direct

Regards.

Mike6979865
Posts: 0
Joined: Fri Jul 11, 2014 3:53 pm

Convert _CreatedAt - Javascript Assistance

I did exactly the same, and I'm getting "Invalid Date." Please see screenshots below:

Image

Image

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Convert _CreatedAt - Javascript Assistance

Hi Mike,

I don't have the same problem,

But here is new code for you:

pre

var stringDate = value;

var arrDate = stringDate.split(/[- :]/);
var date = new Date(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3], arrDate[4]);
console.log(date);

return date.toUTCString();

/pre

Regards.

Mike6979865
Posts: 0
Joined: Fri Jul 11, 2014 3:53 pm

Convert _CreatedAt - Javascript Assistance

Hi Yurii,

This code is returning GMT time. I'm interested in Pacific Standard Time. Your assistance is highly appreciated.

Regards,

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Convert _CreatedAt - Javascript Assistance

Hi Mike,

Here is ready code with changing time to certain time-zone:

pre

var stringDate = value;
var arrDate = stringDate.split(/[- :]/);
var date = new Date(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3], arrDate[4]);

//You can change timezone here;
var goalTimeZone = -7;
date = new Date(date.getTime() + (goalTimeZone * 3600 * 1000));

console.log(date);
return date.toUTCString();

/pre

Regards.

Mike6979865
Posts: 0
Joined: Fri Jul 11, 2014 3:53 pm

Convert _CreatedAt - Javascript Assistance

Hi Yurii,
This code is still showing GMT time. I know that PDT is -7, so this code looks correct. I don't know why it's showing GMT time.

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Convert _CreatedAt - Javascript Assistance

Hi Mike,

You can just remove "GMT" part of this text:

pre

var stringDate = value;
var arrDate = stringDate.split(/[- :]/);
var date = new Date(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3], arrDate[4]);
//You can change timezone here;
var goalTimeZone = -7;
date = new Date(date.getTime() + (goalTimeZone * 3600 * 1000));
console.log(date);
return date.toUTCString().replace(/GMT/gi, "");

/pre

Regards.

Return to “Issues”