Skip to content

Instantly share code, notes, and snippets.

@mrinalwadhwa
Last active October 21, 2019 11:37
Show Gist options
  • Save mrinalwadhwa/2ceb54c146617c8a16cdb9d34fd0981c to your computer and use it in GitHub Desktop.
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.
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
Copy link
Author

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

@mrinalwadhwa
Copy link
Author

YYYY-MM-DDTHH:MM:SS.MMMZ

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