Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!
SOLVED

Converting String date to Date in JavaScript

Avatar

Level 3

All, 

 

I created a Javascript where I ran a xtk.QueryDef and pulled in a "Date" object.  

montezh2001_0-1642652484065.png

 

I see the @mailDate is now converted to a String. My issue is, I need to be able to use the date in an "IF-ELSE" statement, and perform calculations on the date (i.e - add months, add year, etc); I need to create a Start Date mm/dd/yyyy and End Date - mm/dd/yyyy - and then be able to compare those dates to another date field in the workflow.  

 

I've been racking my brain on trying to convert this string to Date field for me to use, and I can not seem to get it done - not with ParseInt or FormatDate (this actually looks at the string and returns the wrong date). I was trying to do the coding in a Javascript and then pass the values on to an Enrichment as an instance variable. 

 

Right now it looks like I will need to use consecutive Enrichments to get the final Start and End Dates I need; unless someone can give me some guidance on how to get this coded. 

 

Thank you

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

You need to change that string into a date before you evaluate it in the loop. What is the format of the current string?

 

Inside your for loop do something like this.

Option 1 - using vanilla JS (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

const myDate = Date.parse(new Date(result.@mailDate));

//then

If(myDate > date2){}

 

Option 2 - using ACC Date function

const myDate = formatDate(result.@mailDate, "%4Y/%2M/%2D")

//then

If(myDate > date2){}

https://experienceleague.adobe.com/developer/campaign-api/api/f-formatDate.html 

 

Option 3 - using moment JS (be aware library still works although it is not being investing in anymore, but it still makes it easier). This helped me when I was doing advanced date manipulation 100+ lines of JS.

https://blog.floriancourgey.com/2018/10/use-javascript-libraries-in-adobe-campaign/ 

 

I've done all 3. Which ever fits your context the best is the right one to use.

View solution in original post

2 Replies

Avatar

Employee Advisor

Hello,

I don't know if that help, but there is some documentation about using dates in Campaign:

https://experienceleague.adobe.com/developer/campaign-api/api/p-5.html

The example says 

var query = NLWS.xtkQueryDef.create(
  {queryDef: {schema: "nms:delivery", operation: "get", 
    select: {
      node: {expr: "@lastModified"}
    }, 
    where: {
      condition: {expr: "@id=123456"}
    }
  }}
)

var delivery = query.ExecuteQuery()
var lastModified = parseTimeStamp(delivery.$lastModified) // <-- parseTimeStamp returns a Date object

https://experienceleague.adobe.com/developer/campaign-api/api/f-parseTimeStamp.html?hl=parsetimestam... 

 

Best regards, Tobias

Avatar

Correct answer by
Employee Advisor

You need to change that string into a date before you evaluate it in the loop. What is the format of the current string?

 

Inside your for loop do something like this.

Option 1 - using vanilla JS (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

const myDate = Date.parse(new Date(result.@mailDate));

//then

If(myDate > date2){}

 

Option 2 - using ACC Date function

const myDate = formatDate(result.@mailDate, "%4Y/%2M/%2D")

//then

If(myDate > date2){}

https://experienceleague.adobe.com/developer/campaign-api/api/f-formatDate.html 

 

Option 3 - using moment JS (be aware library still works although it is not being investing in anymore, but it still makes it easier). This helped me when I was doing advanced date manipulation 100+ lines of JS.

https://blog.floriancourgey.com/2018/10/use-javascript-libraries-in-adobe-campaign/ 

 

I've done all 3. Which ever fits your context the best is the right one to use.