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

LetterDescriptionExample
GEra designatorAD
yYear in calendar2025 (yyyy); 25 (yy)
MMonth in yearJuly (MMMM); Jul (MMM); 07 (MM), 7 (M)
wWeek in year27 (27th week of the year)
WWeek in month2 (second week of the month)
DDay in year10
dDay in month189
EDay of weekTuesday (EEEE); Tue (E, EE, EEE)
FDay of week in month2 (second occurrence of the weekday in the month)
uDay number of week (1=Monday, …, 7=Sunday)1
aAM/PM markerPM
HHour in day (0-23)0
kHour in day (1-24)24
KHour in AM/PM (0-11)0
hHour in AM/PM (1-12)12
mMinute in hour30
sSecond in minute55
SMillisecond978
zTime zone (general form)Pacific Standard Time; PST
ZTime zone (RFC 822 form)-0800
XTime 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 codeDescription
90820The date format string is invalid.
90821The locale is not a string constant.

Usage examples

  1. 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
    
  2. 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
    
  3. 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
    
  4. 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.