Ruby:
Getting the current date

How to:

Ruby’s standard library includes the Date and Time classes for handling dates and time. Here’s how to get the current date:

require 'date'

current_date = Date.today
puts current_date

Sample output:

2023-04-12

For including time with the date, Ruby’s Time class is more suitable:

current_time = Time.now
puts current_time

Sample output:

2023-04-12 14:33:07 +0200

If you need more functionality, such as time zone management, you might want to use a third-party gem like ActiveSupport (part of Rails but can be used standalone).

First, add activesupport to your Gemfile and run bundle install:

gem 'activesupport'

Then, use it to handle time zones:

require 'active_support/time'

Time.zone = 'Eastern Time (US & Canada)'  # Set your desired time zone
current_time_with_zone = Time.zone.now
puts current_time_with_zone

Sample output:

Wed, 12 Apr 2023 08:33:07 EDT -04:00