ksqlDB provides date and time functions that you can use to work with timestamps in Kafka streams and tables. In this article, we will use UNIX_TIMESTAMP(), FROM_UNIXTIME(), and FORMAT_TIMESTAMP() to obtain and format the current timestamp.

Quick answer: Use UNIX_TIMESTAMP() when you need the current time as milliseconds since the Unix epoch. Use FROM_UNIXTIME() to convert that value to a timestamp, and FORMAT_TIMESTAMP() when you need a formatted string.

Get the current timestamp

SELECT UNIX_TIMESTAMP() AS current_timestamp
FROM your_stream
EMIT CHANGES;
Get the current timestamp in Confluent ksqlDB

UNIX_TIMESTAMP() returns the current time as the number of milliseconds since the Unix epoch.

Convert the value to TIMESTAMP

Use FROM_UNIXTIME() when you want to convert the Unix timestamp value to a ksqlDB TIMESTAMP.

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP()) AS current_timestamp
FROM your_stream
EMIT CHANGES;
Convert the current Unix timestamp to TIMESTAMP in ksqlDB

Format the timestamp

Use FORMAT_TIMESTAMP() to return the timestamp in a specific display format.

SELECT FORMAT_TIMESTAMP(FROM_UNIXTIME(UNIX_TIMESTAMP()), 'yyyy-MM-dd HH:mm:ss')
FROM your_stream
EMIT CHANGES;

Pro tips:
Use UNIX_TIMESTAMP() for epoch milliseconds, FROM_UNIXTIME() for a TIMESTAMP value, and FORMAT_TIMESTAMP() when you need presentation formatting. For automated ksqlDB deployments, see our ksql-migrations CI/CD guide.

See more

Visual Studio Marketplace

SSIS Catalog Migration Wizard

Extend Visual Studio with an easy way to migrate SSIS Catalog projects.

Kunal Rathi

With over 15 years of experience in data engineering and analytics, I've assisted countless clients in gaining valuable insights from their data. As a dedicated supporter of Data, Cloud and DevOps, I'm excited to connect with individuals who share my passion for this field. If my work resonates with you, we can talk and collaborate.