Date Functions and Properties
Overview
Date functions in SnapLogic work similarly to JavaScript Date objects, where a Date represents the number of milliseconds since midnight January 1, 1970 UTC (the Unix epoch). The Date object provides methods for parsing, formatting, and manipulating date and time values in your pipelines.
Comparing Dates
Dates can be compared using the relational operators (>, >=, <, <=, ==). For example:
Date.parse("2011-10-10T14:48:00.123-08:00") > Date.parse("2011-10-10T12:48:00.123-08:00") // true
// The following is true because a Date is a number of milliseconds since midnight January 1, 1970 UTC
Date.parse("2011-10-10T15:48:00.123-08:00") > 1318286880123 // true
Creating Date Objects
Date.now()
Returns the current datetime as YYYY-MM-DDTHH:mm:ss.SSS UTC. The results are of the date-time data type.
Syntax:
Date.now()
Example:
Date.now() // Returns: 2017-02-21T21:34:22.025 UTC
Date.parse()
Parses a string representation of a date, and returns it as YYYY-MM-DDTHH:mm:ss.SSS that matches the server's timezone. The results are of the date-time data type.
Syntax:
Date.parse(dateString)
Date.parse(millisFromEpoch)
Date.parse(dateString, format)
Date.parse(dateNumber, format)
Parameters:
dateString- A string representation of a datemillisFromEpoch- Number of milliseconds from the epoch (Jan 1, 1970)format- Custom format string using Java SimpleDateFormat syntaxdateNumber- A number to be converted to a string and parsed using the given format
Examples:
Date.parse(1445910469510) // Returns: 2015-10-27T01:47:49.510 (in server's timezone)
Date.parse("2011 10 30", "yyyy MM dd") // Returns: 2011-10-30T00:00:00.000
Date.parse($['System Modstamp'], "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'") // Returns: 2015-04-15T13:23:10.000
Date.parse(20160923, "yyyyMMdd") // Returns: 2016-09-23T00:00:00.000
Date.UTC()
Returns the date/time in universal time. The results are of the date-time data type.
Syntax:
Date.UTC(year, month, [day, hour, minute, second, millisecond])
Parameters:
year- A year after 1900month- An integer between 0 and 11 representing the monthday(optional) - An integer between 1 and 31 representing the day of the monthhour(optional) - An integer between 0 and 23 representing the hoursminute(optional) - An integer between 0 and 59 representing the minutessecond(optional) - An integer between 0 and 59 representing the secondsmillisecond(optional) - An integer between 0 and 999 representing the milliseconds
Example:
Date.UTC(2015, 7, 20, 13, 23, 12, 34) // Returns: 2015-08-20T13:23:12.034 UTC
Local Date/Time Parsing
LocalDateTime.parse()
Returns the date in local time.
Syntax:
LocalDateTime.parse(dateString)
Example:
LocalDateTime.parse("2011-10-31") // Returns: 2011-10-31T00:00:00.000
LocalDate.parse()
Returns the date in local time without time component.
Syntax:
LocalDate.parse(dateString)
Example:
LocalDate.parse("2011/10/31") // Returns: 2011-10-31
LocalTime.parse()
Returns the time in local time without date component.
Syntax:
LocalTime.parse(timeString)
Example:
LocalTime.parse("23:42:00") // Returns: 23:42:00.000
Getter Methods
The following methods extract specific components from a Date object according to local time unless noted otherwise.
| Method | Description | Return Value Range |
|---|---|---|
getDate() |
Day of the month | 1 to 31 |
getDay() |
Day of the week (0 = Sunday) | 0 to 6 |
getFullYear() |
Four-digit year | Integer year |
getHours() |
Hour of the day | 0 to 23 |
getMilliseconds() |
Milliseconds | 0 to 999 |
getMinutes() |
Minutes | 0 to 59 |
getMonth() |
Month (1 = January) - SnapLogic-specific | 1 to 12 |
getMonthFromZero() |
Month (0 = January) - JavaScript-compatible | 0 to 11 |
getSeconds() |
Seconds | 0 to 59 |
getTime() |
Milliseconds since epoch | Number |
Examples:
Date.parse("2014-09-22").getDate() // Returns: 22
Date.parse("2014-09-22").getDay() // Returns: 1 (Monday)
Date.now().getFullYear() // Returns: 2017
Date.now().getHours() // Returns: 21
Date.parse("2019-03-04T17:32:53.077 UTC").getMonth() // Returns: 3
getMonth() returns 1-12 unlike JavaScript which returns 0-11. Use getMonthFromZero() for JavaScript-compatible behavior.UTC Getter Methods
These methods return date components according to UTC time rather than local time.
| Method | Description | Return Value Range |
|---|---|---|
getUTCDate() |
Day of the month in UTC | 1 to 31 |
getUTCDay() |
Day of the week in UTC | 0 to 6 |
getUTCFullYear() |
Year in UTC | 1000 to 9999 |
getUTCHours() |
Hours in UTC | 0 to 23 |
getUTCMinutes() |
Minutes in UTC | 0 to 59 |
getUTCSeconds() |
Seconds in UTC | 0 to 59 |
getUTCMilliseconds() |
Milliseconds in UTC | 0 to 999 |
getUTCMonth() |
Month in UTC (1 = January) | 1 to 12 |
getUTCMonthFromZero() |
Month in UTC (0 = January) | 0 to 11 |
getTimezoneOffset() |
Time difference from UTC in minutes | Number |
Examples:
Date.parse("2019-03-04T17:32:53.077").getUTCDate() // Returns: 4
Date.parse("2019-03-04T17:32:53.077").getUTCMonth() // Returns: 3
Date.parse("2019-03-04T17:32:53.077").getUTCHours() // Returns: 17
Date.parse("2019-03-04T17:32:53.077").getTimezoneOffset() // Returns: 0
Conversion Methods
toString()
Returns a string representing the specified Date object.
Date.parse("2014-09-22").toString() // Returns: 2014-09-22T00:00:00.000Z
toLocaleString()
Returns a string with a language-sensitive representation of the date and time.
Options:
format- Format string using Java SimpleDateFormat syntaxtimeZone- Target time zone (Joda-Time Canonical ID)locale- Language tag (e.g., "en-US", "ar-EG", "fa-IR")
Date.now().toLocaleString({"locale":"ar-EG"})
Date.now().toLocaleString({"locale":"en-US"})
toLocaleDateString()
Returns a string with a language-sensitive representation of the date portion only.
Date.now().toLocaleDateString() // Returns: 2014-09-22
Date.now().toLocaleDateString({"format":"MM-dd-yyyy"}) // Returns: 09-22-2014
Date.now().toLocaleDateString({"locale":"ar-EG"})
toLocaleDateTimeString()
Returns a string with a language-sensitive representation combining date and time.
Date.parse("2024-03-29").toLocaleDateTimeString() // Returns: 2024-03-29T00:00:00.000
Date.parse("2024-03-28T16:23:59.825").toLocaleDateTimeString({
"timeZone":"US/Eastern",
"format":"yyyy-MM-dd HH:mm a"
}) // Returns: 2024-03-28 12:23 PM
toLocaleTimeString()
Returns a string with a language-sensitive representation of the time portion only.
Date.parse("2014-09-22").toLocaleTimeString() // Returns: 00:00:00.000
Date.now().toLocaleTimeString({"locale":"en-US"})
Best Practices
- Time Zones: Always be explicit about time zones when parsing or formatting dates. Use UTC for data exchange between systems.
- Format Consistency: Use consistent date formats throughout your pipeline to avoid parsing errors.
- Database Compatibility: Be aware of database-specific date formats. See Database Date Types documentation for details.
- Month Indexing: Remember that
getMonth()returns 1-12 in SnapLogic, but JavaScript uses 0-11. UsegetMonthFromZero()for JavaScript compatibility. - Immutability: Date objects are immutable. Operations return new Date objects rather than modifying the original.
- Custom Formats: Use Java SimpleDateFormat syntax for custom date formatting. Reference Joda-Time DateTimeFormat for full details.
- Locale Support: Leverage locale options for internationalization rather than manually formatting dates for different regions.
Common Patterns
Current Timestamp:
Date.now() // Current datetime in UTC
Parse Custom Format:
Date.parse($dateField, "MM/dd/yyyy HH:mm:ss")
Format for Display:
Date.now().toLocaleDateString({"format":"MMMM dd, yyyy"})
Convert to Unix Timestamp:
Date.now().getTime() // Milliseconds since epoch
Compare Dates:
$expirationDate > Date.now() ? "Active" : "Expired"
Extract Components:
// Get year, month, day from a date
{
"year": $myDate.getFullYear(),
"month": $myDate.getMonth(),
"day": $myDate.getDate()
}
Time Zone Conversion:
$utcDate.toLocaleDateTimeString({
"timeZone": "America/New_York",
"format": "yyyy-MM-dd HH:mm:ss z"
})