One of my most common use cases for SSJS is making API calls in Automation Studio using the Script Activity. Building an automation that can retrieve or refresh data from an external source can open up so many amazing communication opportunities.
You could get the local weather data feed and use it to advertise weather appropriate products or destinations – I actually did a video on this here: https://www.youtube.com/watch?v=vCSCnoiR5ww
You could get up to date price data on hotels or flights to identified cheap travel opportunities; or even query the daily movement of bitcoin for your subscribers!
Step 1: Getting a Date value
var static_date = "January 1, 2021"; var de_date = Platform.Function.Lookup('CustomerData','DateofBirth',['FirstName','LastName'],['Angela','Ruiz']; var Current_date = new Date();
Step 2: Convert String to a Date
Lets use the date values we captured above. We can use the following JS codes to convert these dates into the correct JS date objects:
var static_date = new Date("January 1, 2021"); var de_date = Platform.Function.Lookup('CustomerData','DateofBirth',['FirstName','LastName'],['Angela','Ruiz']; var new_de_date = new Date(de_date); var Current_date = new Date();
Step 3: Altering a Date
static_date.setDate(static_date.getHours()-6); //subtracts 6 hours from the "static_date" value new_de_date.setDate(new_de_date.getDate()+30); //adds 30 days to the "new_de_date" value Current_date.setMonth(13); //adds 1 year to the "Current_date" value
Note that at the time of writing, Salesforce Marketing Cloud has a problem with the “setFullYear()” function, however the “setMonth(13)” workaround shown above is a suitable way to add years to your dates.
Step 4: Output your Date value
Now it’s time to output the date value into your code. I’ve seen a number of different date format requirements in APIs, so lets step through a little trick I use to make date formats as easy as possible.
Lets assume we need to produce the following date structure for our API call: 2021-05-14 00:00:00
The current date that we have in our date value is: Fri, 14 May 2021 20:58:13 GMT-06:00
So we can manually output the date parts in the correct format by using the following codes:
var NowDate = new Date("Fri, 14 May 2021 20:58:13 GMT-06:00"); var output= NowDate.getFullYear()+'-'+("0"+(NowDate.getMonth()+1)).slice(-2)+'-'+NowDate.getDate()+" 00:00:00"; //Output: 2021-05-14 00:00:00
What the code is doing:
Firstly we are setting the “NowDate” value using new Date();
Next we are building the “output” value by concatenating each of the date parts as we need them:
getFullYear() will return the YYYY date part.
getMonth() will return the month number from 0 to 11, so we need to +1. Some APIs enforce a 2-digit month value, so we can add a “0” to the front and use the slice() function to return only the last 2 characters in the string.
The getDate() function will give us the date from 1 to 31.
And lastly the ” 00:00:00″ is added to be compliant with the example date time format, assuming we wish to get data starting from midnight. The getHours(), getMinutes(), and getSeconds() functions could have been used to specify these dynamically if required.
I’ve seen some creative examples of people using AMPscript to set and alter the date values before using them in SSJS – however working with Dates in SSJS doesn’t need to be a burden. Below is a copy of my code snippet that I use for quickly building dates for API calls:
//Format: YYYY-MM-DD 00:00:00 var Today = new Date(); var Yesterday = new Date(); Yesterday.setDate(Yesterday.getDate()-1); var Today_string = Today.getFullYear()+'-'+("0"+(Today.getMonth()+1)).slice(-2)+'-'+Today.getDate()+" 00:00:00"; var Yesterday_string = Yesterday.getFullYear()+'-'+("0"+(Yesterday.getMonth()+1)).slice(-2)+'-'+Yesterday.getDate()+" 00:00:00";