Custom JS to extract YYYY from a data element text and subtract from current year to find out age in years | Community
Skip to main content
July 7, 2020
Solved

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

  • July 7, 2020
  • 1 reply
  • 1127 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by adilk1

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

 

Answer above.

1 reply

adilk1AuthorAccepted solution
July 11, 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.

jantzen_b
Adobe Employee
Adobe Employee
September 14, 2020
Thanks for following up with your own solution. I've marked it correct so others can easily find it.