ruby get current datetime utc
To get the current datetime in UTC using Ruby, you can follow these steps:
- Import the necessary library: Begin by importing the
Time
class from the Ruby standard library. This class provides various methods for working with dates and times.
require 'time'
- Get the current datetime: Use the
now
method of theTime
class to get the current datetime.
current_datetime = Time.now
- Convert to UTC: To convert the current datetime to UTC, use the
utc
method.
current_datetime_utc = current_datetime.utc
By following these steps, you can obtain the current datetime in UTC using Ruby.