Ruby | DateTime to_time() function Last Updated : 09 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report DateTime#to_time() : to_time() is a DateTime class method which returns the time object which denotes the DateTime object self. Syntax: DateTime.to_time() Parameter: DateTime values Return: duplicate DateTime object self and resets its to_time. Example #1 : Ruby # Ruby code for DateTime.to_time() method # loading library require 'date' # declaring DateTime value date_a = DateTime.new(2019, 8, 10, 4, 10, 9) # declaring DateTime value date_b = DateTime.new(2019, 8, 10.5) # declaring DateTime value date_c = DateTime.new(2019, 8, 10, 4, 10, 9, Rational(4, 24)) # to_time method puts "DateTime to_time form : #{date_a.to_time}\n\n" puts "DateTime to_time form : #{date_b.to_time}\n\n" puts "DateTime to_time form : #{date_c.to_time}\n\n" Output : DateTime to_time form : 2019-08-10 06:10:09 +0200 DateTime to_time form : 2019-08-10 14:00:00 +0200 DateTime to_time form : 2019-08-10 02:10:09 +0200 Example #2 : Ruby # Ruby code for DateTime.to_time() method # loading library require 'date' # declaring DateTime value date_a = DateTime.new(2019, 8, 10, 5) # declaring DateTime value date_b = DateTime.parse('10 Aug 2018 04:10:06+04:30') # declaring DateTime value date_c = DateTime.new(2019, 8, 10, 4, 10, 9, '+03:00') # to_time method puts "DateTime to_time form : #{date_a.to_time}\n\n" puts "DateTime to_time form : #{date_b.to_time}\n\n" puts "DateTime to_time form : #{date_c.to_time}\n\n" Output : DateTime to_time form : 2019-08-10 07:00:00 +0200 DateTime to_time form : 2018-08-10 01:40:06 +0200 DateTime to_time form : 2019-08-10 03:10:09 +0200 Comment More infoAdvertise with us Next Article Ruby | DateTime strptime() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby DateTime-class Similar Reads Ruby | Time to_datetime() function Time#to_datetime() is a Time class method which returns a date time object of itself. Syntax: Time.to_datetime() Parameter: Time values Return: a date time object of itself. Example #1 : Ruby # Ruby code for Time.to_datetime() method # loading library require 'time' # declaring time a = Time.new(201 2 min read Ruby | Time to_date() function Time#to_date() is a Time class method which returns a self Date object. Syntax: Time.to_date() Parameter: Time values Return: a self Date object. Example #1 : Ruby # Ruby code for Time.to_date() method # loading library require 'time' # declaring time a = Time.new(2019) # declaring time b = Time.new 2 min read Ruby | DateTime to_s() function DateTime#to_s() : to_s() is a DateTime class method which returns string representation of the DateTime object. Syntax: DateTime.to_s() Parameter: DateTime values Return: string representation of the DateTime object. Example #1 : Ruby # Ruby code for DateTime.to_s() method # loading library require 2 min read Ruby | DateTime strptime() function DateTime#strptime() : strptime() is a DateTime class method which parses the given representation of date and time with the given template Syntax: DateTime.strptime() Parameter: DateTime values Return: parses the given representation of date and time with the given template Example #1 : Ruby # Ruby 1 min read Ruby | DateTime strftime() function DateTime#strftime() : strftime() is a DateTime class method which returns Formats date to the directives in the given format string. Syntax: DateTime.strftime() Parameter: DateTime values Return: ate according to the directives in the given format string. Example #1 : Ruby # Ruby code for DateTime.s 2 min read Ruby | Time to_a() function Time#to_a() is a Time class method which returns the string which returns a ten-element array of values for time. Format [sec, min, hour, day, month, year, wday, yday, isdst, zone] Syntax: Time.to_a() Parameter: Time values Return: a ten-element array of values for time. Example #1 : Ruby # Ruby cod 2 min read Like