Expand my Community achievements bar.

Help shape the future of AI assistance by participating in this quick card sorting activity. Your input will help create a more effective system that better serves your needs and those of your colleagues.
SOLVED

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

Avatar

Level 1

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.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 1

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

 

Answer above.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 1

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

 

Answer above.

Avatar

Level 10
Thanks for following up with your own solution. I've marked it correct so others can easily find it.