Custom JS to extract YYYY from a data element text and subtract from current year to find out age in years

adilk1

07-07-2020

Hello,

I have a data element that is currently picking up values such as this:

Tue Oct 07 1975 00:00:00 GMT+0400 (Gulf Standard Time)

Tue May 05 1981 00:00:00 GMT+0400 (Gulf Standard Time)

 

I'd like to extract the YYYY [Char 11 - 15] from the text and then subtract from current year 2020 to get the age of the user. How can I accomplish this?

Output required:

Case 1: 45 ... [2020 - 1975]

Case 2: 39 ...[2020 - 1981]

 

--

Edit: This is what I have so far but it doesn't load the data element.

QB: DOB is for example: Tue Oct 07 1975 00:00:00 GMT+0400 (Gulf Standard Time) 

--

Here's what I think it should do:

1. Extract the 4 digits as string

2. Convert the 4 digit to integer

3. getFullYear to get 2020 

4. Subtract the 4 digit integer from 2020 to pass the value to Adobe Analytics.

--

Current code:

var x=_satellite.getVar('QB: DOB');
var y= x.slice(11,15);
return y;

--

 

Can anyone help with this.

Thanks.

custom javascript custom js Data Elements

Accepted Solutions (1)

Accepted Solutions (1)

adilk1

11-07-2020

var x=_satellite.getVar('dataLayer var: DOB'); var dobYear = x.getFullYear(); var curYear = new Date().getFullYear(); var age = curYear - dobYear; return age;

 

Answer above.

Answers (0)