Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby: Convert to UTC time in ISO 8601 format with millisecond precision, padding of 3.
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"
@mrinalwadhwa

This comment has been minimized.

Copy link
Owner Author

@mrinalwadhwa mrinalwadhwa commented Sep 2, 2016

utc_iso8601_milli Time.new(2016, 5, 24, 15, 29, 22.0039)
# => "2016-05-24T09:59:22.003Z"
@mrinalwadhwa

This comment has been minimized.

Copy link
Owner Author

@mrinalwadhwa mrinalwadhwa commented Sep 2, 2016

YYYY-MM-DDTHH:MM:SS.MMMZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.