Ruby Tutorial
Ruby CGI
Ruby Advanced
In Ruby, you can handle dates and times using built-in classes like Time
, Date
, and DateTime
.
1. Time:
The Time
class represents dates and times in Ruby. You can create a new Time
object that represents the current time with the Time.now
method:
time = Time.now puts time
You can access various components of the time:
puts time.year puts time.month puts time.day puts time.hour puts time.min puts time.sec
2. Date:
To use the Date
class, you need to require the 'date' library. The Date
class handles dates (year, month, day), but not times.
require 'date' date = Date.today puts date
You can access different parts of the date:
puts date.year puts date.month puts date.day
3. DateTime:
DateTime
is a subclass of Date
that easily handles dates and times. It's more precise than Time
for historical dates.
require 'date' datetime = DateTime.now puts datetime
And you can access different parts of the DateTime
:
puts datetime.year puts datetime.month puts datetime.day puts datetime.hour puts datetime.min puts datetime.sec
4. Parsing Dates and Times:
You can parse dates and times from strings:
require 'date' date = Date.parse('2023-05-16') puts date datetime = DateTime.parse('2023-05-16T20:15:30') puts datetime
5. Formatting Dates and Times:
You can format dates and times as strings. The strftime
method allows you to specify a format string:
time = Time.now puts time.strftime('%Y-%m-%d %H:%M:%S') date = Date.today puts date.strftime('%Y-%m-%d') datetime = DateTime.now puts datetime.strftime('%Y-%m-%d %H:%M:%S')
In these examples, %Y
represents the four-digit year, %m
represents the two-digit month, %d
represents the two-digit day, %H
represents the two-digit hour, %M
represents the two-digit minute, and %S
represents the two-digit second.
These are the basics of handling dates and times in Ruby. There are many more methods available in the Time
, Date
, and DateTime
classes for more advanced manipulations.
Ruby DateTime class:
DateTime
class in Ruby is part of the date
standard library and provides functionality for working with dates and times.require 'date' my_datetime = DateTime.now puts my_datetime
Working with dates in Ruby:
Date
class for working specifically with dates.require 'date' my_date = Date.new(2023, 12, 23) puts my_date
Ruby Time class:
Time
class in Ruby represents points in time.my_time = Time.now puts my_time
Formatting dates in Ruby:
strftime
method to format dates and times in Ruby.my_time = Time.now formatted_time = my_time.strftime("%Y-%m-%d %H:%M:%S") puts formatted_time
Parsing dates in Ruby:
DateTime
or Time
objects using the DateTime.parse
or Time.parse
methods.require 'date' my_date = DateTime.parse("2023-12-23") puts my_date
Calculating time differences in Ruby:
-
operator to calculate time differences.start_time = Time.now # Perform some operations or tasks end_time = Time.now time_difference = end_time - start_time puts "Time difference: #{time_difference} seconds"
Ruby strftime method:
strftime
method allows you to format Time
or DateTime
objects according to a specified format.my_time = Time.now formatted_time = my_time.strftime("%Y-%m-%d %H:%M:%S") puts formatted_time
Adding and subtracting time in Ruby:
+
and -
operators to add or subtract time from Time
or DateTime
objects.my_time = Time.now future_time = my_time + 3600 # Add one hour past_time = my_time - 3600 # Subtract one hour
Ruby Date and Time zones:
TZInfo
gem or the built-in DateTime.new_offset
method.require 'date' my_date = DateTime.now date_in_utc = my_date.new_offset(0) puts date_in_utc
Time formatting in Ruby:
%
directives in the strftime
method.my_time = Time.now formatted_time = my_time.strftime("%Y-%m-%d %H:%M:%S") puts formatted_time
Ruby Date and Time libraries:
Date
and Time
for working with dates and times.require 'date' my_date = Date.new(2023, 12, 23) puts my_date my_time = Time.now puts my_time
Time arithmetic in Ruby:
Time
objects.my_time = Time.now future_time = my_time + 3600 # Add one hour past_time = my_time - 3600 # Subtract one hour
Converting between time zones in Ruby:
TZInfo
gem or DateTime.new_offset
to convert between time zones.require 'date' require 'tzinfo' my_date = DateTime.now timezone = TZInfo::Timezone.get('America/New_York') date_in_ny = my_date.new_offset(timezone.utc_offset) puts date_in_ny
Getting the current date and time in Ruby:
Time.now
or DateTime.now
to get the current date and time.my_time = Time.now my_date = DateTime.now