Skip to content

Instantly share code, notes, and snippets.

@dstagner
Last active June 11, 2025 14:47
Show Gist options
  • Save dstagner/193207fed46acf5b5bae to your computer and use it in GitHub Desktop.
Save dstagner/193207fed46acf5b5bae to your computer and use it in GitHub Desktop.
Ruby ISO8601 time in milliseconds

So you want to generate ISO8601 formatted timestamps in Ruby, but need resolution finer than a second?

First, make sure to require 'time' in order to get ISO8601 formatting. But that gets you this:

2.1.0 :001 > require 'time'
 => true 
2.1.0 :002 > Time.now.utc.iso8601
 => "2015-11-12T04:46:43Z" 

The round() method will get you finer resolution!

2.1.0 :003 > Time.now.utc.round(10).iso8601(6)
=> "2015-11-12T04:47:55.196329Z" 
@will-clarke
Copy link

🎉 Thanks!

@Valve
Copy link

Valve commented Mar 1, 2019

thanks!

@W-Mills
Copy link

W-Mills commented Apr 27, 2023

❤️ thanks!

@joshuapinter
Copy link

Love this. Just to reiterate, you only need to specify the number of decimals in .iso8601. You don't need/want round.

Time.current.utc.iso8601( 3 )
#=> "2025-06-11T14:45:48.072Z"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment