Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

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

Avatar

Avatar
Boost 1
Level 1
adilk1
Level 1

Likes

2 likes

Total Posts

2 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Affirm 1
View profile

Avatar
Boost 1
Level 1
adilk1
Level 1

Likes

2 likes

Total Posts

2 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Affirm 1
View profile
adilk1
Level 1

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)

Avatar

Avatar
Boost 1
Level 1
adilk1
Level 1

Likes

2 likes

Total Posts

2 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Affirm 1
View profile

Avatar
Boost 1
Level 1
adilk1
Level 1

Likes

2 likes

Total Posts

2 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Affirm 1
View profile
adilk1
Level 1

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)