date()
Converts a string to a timestamp type according to a specified format.
Syntax
date(DATE_EXPR, DATE_FMT[, LOCALE])
Parameters
DATE_EXPR- The string or expression to convert to a timestamp type.
DATE_FMT- A date parsing format string as defined by Java's SimpleDateFormat class. The following date pattern letters are supported.
Date pattern letters
Letter Description Example G Era designator AD y Year in calendar 2025 ( yyyy); 25 (yy)M Month in year July ( MMMM); Jul (MMM); 07 (MM), 7 (M)w Week in year 27 (27th week of the year) W Week in month 2 (second week of the month) D Day in year 10 d Day in month 189 E Day of week Tuesday ( EEEE); Tue (E,EE,EEE)F Day of week in month 2 (second occurrence of the weekday in the month) u Day number of week (1=Monday, …, 7=Sunday) 1 a AM/PM marker PM H Hour in day (0-23) 0 k Hour in day (1-24) 24 K Hour in AM/PM (0-11) 0 h Hour in AM/PM (1-12) 12 m Minute in hour 30 s Second in minute 55 S Millisecond 978 z Time zone (general form) Pacific Standard Time; PST Z Time zone (RFC 822 form) -0800 X Time zone (ISO 8601 form) -08;-0800;08:00 LOCALE- A two- or three-character locale code as specified in ISO 639 (default:
en). Refer to the ISO 639 code tables for locale codes.
Description
The date() function parses the string using the format specified in DATE_FMT and returns it as a timestamp type. If DATE_EXPR is null or an empty string, returns null. If parsing fails, also returns null. If an array or list is passed, the conversion is applied recursively to each element.
Logpresso does not strictly validate date value ranges. For example, month 13 is treated as January of the following year, and day 32 rolls over to the next month.
Error codes
| Error code | Description |
|---|---|
| 90820 | The date format string is invalid. |
| 90821 | The locale is not a string constant. |
Usage examples
-
Basic date string conversion
json "{'val': '2024-06-10 00:30:55.978'}" | eval result = date(val, "yyyy-MM-dd HH:mm:ss.SSS") | # result: 2024-06-10 00:30:55+0900 -
ISO 8601 format conversion
json "{'val': '2024-01-30T10:11:12.123Z'}" | eval result = date(val, "yyyy-MM-dd'T'HH:mm:ss.SSSX") | # result: 2024-01-30 19:11:12+0900 -
Date conversion using a Korean locale
json "{'val': '6월 1 토요일 2024 12:34:56'}" | eval result = date(val, "MMM dd EEEE yyyy HH:mm:ss", "ko") | # result: 2024-06-01 12:34:56+0900 -
NULL input
json "{'val': null}" | eval result = date(val, "yyyy-MM-dd") | # result: null
Compatibility
The date() function has been available since before Sonar 4.0.