Using dates

This chapter details the functions to use for manipulating dates in the JavaScript programs executed on the server (workflow, JSSP, etc.).

Dates and time zones

In JavaScript, dates are stored un UTC. This means you only need to worry about time zones for date construction or conversion into text strings for internal display or storage For date conversion, there are two function categories:
  • the functions of the JavaScript library (date.toString(), date.toLocaleString(), date.getHours(), ...),
  • the functions of the Adobe Campaign platform ( formatDate(date, format) ).
By convention, the first use the time zone of the machine, while the last use a time zone depending on context: usually that of the current operator, but this behavior can be modified for exports or workflows, for instance.

Obtain current date

There are two ways of obtaining the current date in JavaScript:
  • new Date() creates a new Date object initialized with the current date. The date coincides with the time of the machine which the script is executed on.
  • getCurrentDate() returns the current date based on the time of the database server.
The getCurrentDate() function is preferable to the first solution, specially when the date is to be compared to other dates from the database, or to be written to the database. This method reduces the risk of discrepancies (up to several minutes) linked to time differences between the application server and the database server

Date display

Converting a date into a character string for display purposes uses the formatDate function.

Converting a date from an XML document

Dates stored in an XML document coming from the Adobe Campaign database are in text form: they can be converted using the parseTimeStamp function. This typically concerns XML documents returned by the ExecuteQuery method. Example :
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)

Inserting a date into an XML document

Whether to write a date in an XML attribute, to save a document ( Write method) or to insert a literal date into the expression of a query, the date will have to be converted into ISO8601 format. There is a method in the xtk:shared/nl.js function library for converting a date into this format: the NL.XTK.formatDateTime(date) method. The library can be loaded using the loadLibrary function:
loadLibrary("xtk:shared/nl.js")
or using the page directive in case of a dynamic JavaScript page (JSSP):
<%@ page import="xtk:shared/nl.js" %>
Warning: you cannot use loadLibrary for all scripts. For instance, you can't change a library of JavaScript functions in an email personalization script. Example : This example shows the construction of a query using a literal date.
loadLibrary("xtk:shared/nl.js")
NL.require('/nl/core/shared/xtk.js')
...

var date = ...

var query = NLWS.xtkQueryDef.create(
  {queryDef: {schema: "nms:delivery", operation: "select", 
    select: {
      node: {expr: "@id"}
    }, 
    where: {
      condition: {expr: "@lastModified > #" + NL.XTK.formatDateTime(date) + "#"}
    }
  }})

var result = query.ExecuteQuery()