How Do I Work with Datetime Data

Data almost always contains temporal details like data creation timestamps, birthdays, invoice dates and the like. Although there's a standard for timestamp representation (ISO 8601), it still comes in many different formats.

In Xplenty, date and time can be represented in either:

  • A datetime field
  • A string field in ISO 8601 format, or in a custom string format
  • A numeric field representing Unix time

Reading temporal data into datetime fields

Based on the source, Xplenty can automatically read temporal data into datetime fields as follows:

  • Database, Amazon Redshift and MongoDB source - their respective data type for date and time (e.g. timestamp, datetime, etc.) 
  • Files (flat files or JSON) storage source - strings that match the ISO 8601 patterns.
  • Rest API source - ISO 8601.

If the temporal data is in a string format (other than ISO 8601) or is a Unix time,  use the function ToDate to convert it into a datetime field, or ParseDate to try and parse a string using multiple custom formats (or get a null datetime in case none of the formats match the input string).

To use literal values, it's best to apply the ToDate function to an ISO 8601 string value. For example: ToDate('2013-10-17T23:59:54.432Z').

Applying functions to temporal data

  You can do a lot with temporal data! Some common usage of datetime functions:

  • Temporal data in string format can be cast to datetime data type using the functions ToDate , ParseDate and ToDateCustomFormats.
  • Datetime expressions can be cast to custom string formats using the function ToString.
  • Datetime expressions can be cast to Unix time using the functions ToUnixTime or ToMilliSeconds.
  • Use AddDuration or SubtractDuration to add or subtract a duration to/from datetime fields.
  • To compare datetime expressions (in a filter component or in a CASE WHEN... expression), use *Between (SecondsBetween, MinutesBetween, DaysBetween, etc.) functions value. For example: DaysBetween(field1,field2)>2 passes the records for which field2 is greater than field1 by 2 days or more.
  • Derive a portion of a datetime format field to an integer field using Get* (GetDay, GetMonth, GetYear, etc.) functions.

Working with time zones

Datetime fields in Xplenty contain time zone information. If your temporal data doesn't contain timezone ("wall-clock" time), it is assigned the default timezone when  placed in a datetime field as defined by the system variable _DEFAULT_TIMEZONE. The variable is set by default to UTC timezone and can be changed as described in  setting variables. You can also assign any timezone to a "wall-clock" time using the ToDate function with the timezone argument.The time zone for datetime fields  can also be switched to another time zone using the function SwitchTimeZone.

Writing temporal data into destinations

A datetime field can be mapped directly to these destination fields:

Note that if the destination column type in a database table does not include all datetime components, only a portion of the datetime will be stored in the destination. For example, when mapping ToDate('2013-10-17T23:59:54.432Z') to a date field, only the date portion will be mapped.

The datetime field can also be stored in its internal format in Cloud Destination flat file or JSON destination types.

Feedback and Knowledge Base