No Results
Displaying Current Time in JavaScript

By using the JavaScript transform, you can add the current date stamp to your data set.

For example, this is the data we have in one JSON feed:

To add a new column containing the current date stamp, we are going to create a JavaScript transform off of this feed.

In the Script tab of the JavaScript transform, we are going to enter the following code:

 

function getAttributes(sourceAttrs, nodeVars, secVars, sourceRecords) {
  var attributes = [];
  sourceAttrs.forEach(function(attrList) {
    attrList.forEach(function(attr) {
      attributes.push(attr);
    });
  });
  // Adding new attribute to display current date
  var sampleAttr = {
    name : 'dateNow', // Alphanumerics and underscores only; no leading, trailing, or adjacent underscores
    type : 'date', // 'string'|'int'|'long'|'number'|'boolean'|'date'
    isId : false, // true|false
    units : null, // 'millis'|'seconds' (if attribute is timestamp)
    format : 'yyyy.mm.dd' // string consisting of valid Java SimpleDateFormat pattern
  };
  attributes.push(sampleAttr);
  return jsAttributesSuccess(attributes, "Example Success");
}
function getRecords(sourceRecords, nodeVars, secVars, attributes) {
  var records = [];
  var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + "/"+ (currentdate.getMonth()+1)  + "/" + currentdate.getFullYear() + " @ "  + currentdate.getHours() + ":"  + currentdate.getMinutes() + ":" + currentdate.getSeconds();
  sourceRecords.forEach(function(recordList) {
    recordList.forEach(function(record) {
      record.dateNow = datetime; //Assigning current date stamp to previously made attribute
      records.push(record);
    });
  });
  return jsRecordsSuccess(records, "Example Success");
}

In the Preview, a new column with the current date (dateNow) will be displayed:

 


Terms | Privacy