Current Time Functions
now()
Returns the current timestamp as of query start. Syntax:DateTime
Examples:
- Constant within a single query execution
- All rows in a query see the same
now()value - Non-deterministic across query executions
- Evaluated once at query start, not per row
src/Functions/now.cpp
now64()
Returns the current timestamp with sub-second precision. Syntax:precision: Number of decimal places (3=milliseconds, 6=microseconds, 9=nanoseconds)timezone: Optional timezone string
DateTime64
Examples:
- High-precision event timestamps
- Performance measurement
- Fine-grained time calculations
src/Functions/now64.cpp
today()
Returns the current date (without time component). Syntax:Date
Examples:
- Returns date in system timezone
- Constant within query execution
- Useful for date comparisons
src/Functions/today.cpp
Date/Time Extraction
to_year()
Extracts the year from a date or timestamp. Syntax:UInt16
Examples:
src/Functions/toYear.cpp
to_month()
Extracts the month (1-12) from a date or timestamp. Syntax:UInt8
Examples:
to_day_of_month()
Extracts the day of the month (1-31). Syntax:UInt8
Examples:
to_day_of_week()
Returns the day of the week (1=Monday, 7=Sunday). Syntax:UInt8
Examples:
to_hour() / to_minute() / to_second()
Extract time components. Syntax:UInt8
Examples:
to_iso_year()
Returns the ISO year number (ISO 8601 week-numbering year). Syntax:Date/Time Conversion
to_date()
Converts a value to Date type. Syntax:to_datetime()
Converts a value to DateTime type. Syntax:to_unix_timestamp()
Converts DateTime to Unix timestamp (seconds since 1970-01-01). Syntax:UInt32
Examples:
from_unix_timestamp()
Converts Unix timestamp to DateTime. Syntax:from_unix_timestamp64_*()
Convert Unix timestamps with sub-second precision. Variants:from_unix_timestamp64_milli(value)- Millisecondsfrom_unix_timestamp64_micro(value)- Microsecondsfrom_unix_timestamp64_nano(value)- Nanoseconds
src/Functions/fromUnixTimestamp64*.cpp
Date/Time Arithmetic
add_years() / add_months() / add_days()
Add time intervals to dates or timestamps. Syntax:src/Functions/addYears.cpp, addMonths.cpp, etc.
subtract_years() / subtract_months() / subtract_days()
Subtract time intervals. Syntax:date_diff()
Calculate the difference between two dates in specified units. Syntax:year, quarter, month, week, day, hour, minute, second
Examples:
src/Functions/dateDiff.cpp
Date/Time Rounding
to_start_of_year()
Round down to the start of the year. Syntax:to_start_of_month()
Round down to the start of the month. Syntax:to_start_of_iso_year()
Round down to the start of the ISO year (first Monday of the year). Syntax:to_last_day_of_month()
Get the last day of the month. Syntax:date_trunc()
Truncate date/time to specified precision. Syntax:year, quarter, month, week, day, hour, minute, second
Examples:
src/Functions/date_trunc.cpp
Date/Time Formatting
format_datetime()
Format a DateTime value as a string. Syntax:%Y- Year (4 digits)%m- Month (01-12)%d- Day (01-31)%H- Hour (00-23)%i- Minute (00-59)%s- Second (00-59)%w- Day of week (0=Sunday)%W- Weekday name
src/Functions/formatDateTime.cpp
parse_datetime()
Parse a string to DateTime. Syntax:src/Functions/parseDateTime.cpp
Timezone Functions
timezone_of()
Returns the timezone of a DateTime value. Syntax:src/Functions/timezoneOf.cpp
to_timezone()
Convert DateTime to a different timezone. Syntax:Time Intervals in Streaming
Interval Arithmetic
Work with INTERVAL expressions:Window Intervals
Use in window functions:Special Time Functions
time_slot()
Round time down to the start of a time slot. Syntax:src/Functions/timeSlot.cpp
make_date()
Construct a Date from year, month, day components. Syntax:src/Functions/makeDate.cpp
Performance Considerations
Timezone Conversions
- Timezone lookups can be expensive
- Cache timezone objects when possible
- Prefer working in UTC internally, convert for display
Date Arithmetic
- Integer arithmetic is faster than date functions
- Use
to_unix_timestamp()for duration calculations - Date functions are optimized but still have overhead
Indexing and Filtering
- Date range filters benefit from indexes
- Use appropriate date granularity (day vs hour vs second)
- Partition tables by date for better performance
Best Practices
- Use appropriate precision: Don’t use
DateTime64unless you need sub-second precision - Prefer UTC internally: Convert to local timezone only for display
- Cache conversions: Don’t repeatedly convert the same values
- Use date_trunc for grouping: More efficient than extracting components
- Leverage window functions: Better than manual time bucketing
Examples: Common Patterns
Recent Events Filter
Daily Aggregation
Hourly Trend Analysis
Time-Based Partitioning
Business Hours Filter
Session Duration Calculation
See Also
- Window Functions - Time-based windowing operations
- Aggregation Functions - Aggregate over time windows
- Functions Overview - All available function categories