Milliseconds since epoch to date time

Hello, I run the timestamp() function on a Neo4j servers and get back a number (e.g. 1738096846309) which I'm assuming is the number of milliseconds since the epoch and now I want to convert this into a human-readably formatted date and time. Is this possible within std? Thanks!

You can use UNIX_EPOCH + Duration::from_millis(n) to get a SystemTime, but the standard library doesn't have date/time formatting. You might like the humantime crate for that, for example using format_rfc3339_millis.

4 Likes

Thank you, this works!

info!("Neo4j is up and running as of {}",
        format_rfc3339_millis(UNIX_EPOCH + Duration::from_millis(timestamp)));

Jiff will also do this for you: Timestamp in jiff - Rust

But if you just want an RFC 3339 formatted datetime for emitting to logs, humantime is probably the lighter weight option.

5 Likes

You could also use chrono's DateTime with its from_timestamp_millis() and to_rfc3339() methods.

1 Like