Last active
October 21, 2019 11:37
-
-
Save mrinalwadhwa/2ceb54c146617c8a16cdb9d34fd0981c to your computer and use it in GitHub Desktop.
Ruby: Convert to UTC time in ISO 8601 format with millisecond precision, padding of 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def utc_iso8601_milli(time) | |
time.utc.strftime '%FT%T.%LZ' | |
end | |
utc_iso8601_milli Time.now | |
# => "2016-09-02T07:06:18.134Z" | |
utc_iso8601_milli Time.new(2002, 10, 31, 2, 2, 2, "+05:00") | |
# => "2002-10-30T21:02:02.000Z" | |
# Millisecond with padding of 3. More precise fractions will be truncated: | |
utc_iso8601_milli Time.new(2016, 5, 24, 15, 29, 0.1239) | |
# => "2016-05-24T09:59:00.123Z" | |
utc_iso8601_milli Time.new(2016, 5, 24, 15, 29, 22.1239) | |
# => "2016-05-24T09:59:22.123Z" |
utc_iso8601_milli Time.new(2016, 5, 24, 15, 29, 22.0039)
# => "2016-05-24T09:59:22.003Z"
YYYY-MM-DDTHH:MM:SS.MMMZ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://ruby-doc.org/core-2.2.2/Time.html
http://idiosyncratic-ruby.com/57-what-the-time.html