Page 1 of 2

Convert _CreatedAt - Javascript Assistance

Posted: Tue Sep 16, 2014 7:26 pm
by Mike6979865

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.


Convert _CreatedAt - Javascript Assistance

Posted: Tue Sep 16, 2014 8:08 pm
by Evgene Karachevtsev

Hello Mike,

Please try this code in mapping:

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


Convert _CreatedAt - Javascript Assistance

Posted: Tue Sep 16, 2014 8:56 pm
by Mike6979865

I'm getting "Invalid Date".


Convert _CreatedAt - Javascript Assistance

Posted: Wed Sep 17, 2014 12:55 am
by Yurii Orishchuk

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.


Convert _CreatedAt - Javascript Assistance

Posted: Wed Sep 17, 2014 3:01 pm
by Mike6979865

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

Image

Image

Image


Convert _CreatedAt - Javascript Assistance

Posted: Wed Sep 17, 2014 10:48 pm
by Yurii Orishchuk

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.


Convert _CreatedAt - Javascript Assistance

Posted: Thu Sep 18, 2014 3:45 pm
by Mike6979865

Hi Yurii,

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

Regards,


Convert _CreatedAt - Javascript Assistance

Posted: Fri Sep 19, 2014 2:21 am
by Yurii Orishchuk

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.


Convert _CreatedAt - Javascript Assistance

Posted: Fri Sep 19, 2014 4:46 pm
by Mike6979865

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


Convert _CreatedAt - Javascript Assistance

Posted: Sun Sep 21, 2014 10:23 pm
by Yurii Orishchuk

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.