Ruby | Time to_datetime() function Last Updated : 06 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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(2019) # declaring time b = Time.new(2019, 10) # declaring time c = Time.new(2019, 12, 31) # Time puts "Time a : #{a}\n\n" puts "Time b : #{b}\n\n" puts "Time c : #{c}\n\n\n\n" # to_datetime form puts "Time a to_datetime form : #{a.to_datetime}\n\n" puts "Time b to_datetime form : #{b.to_datetime}\n\n" puts "Time c to_datetime form : #{c.to_datetime}\n\n" Output : Time a : 2019-01-01 00:00:00 +0000 Time b : 2019-10-01 00:00:00 +0000 Time c : 2019-12-31 00:00:00 +0000 Time a to_datetime form : 2019-01-01T00:00:00+00:00 Time b to_datetime form : 2019-10-01T00:00:00+00:00 Time c to_datetime form : 2019-12-31T00:00:00+00:00 Example #2 : Ruby # Ruby code for Time.to_datetime() method # loading library require 'time' # declaring time a = Time.now # declaring time b = Time.new(1000, 10, 10) # declaring time c = Time.new(2020, 12) # Time puts "Time a : #{a}\n\n" puts "Time b : #{b}\n\n" puts "Time c : #{c}\n\n\n\n" # to_datetime form puts "Time a to_datetime form : #{a.to_datetime}\n\n" puts "Time b to_datetime form : #{b.to_datetime}\n\n" puts "Time c to_datetime form : #{c.to_datetime}\n\n" Output : Time a : 2019-08-27 12:27:23 +0000 Time b : 1000-10-10 00:00:00 +0000 Time c : 2020-12-01 00:00:00 +0000 Time a to_datetime form : 2019-08-27T12:27:23+00:00 Time b to_datetime form : 1000-10-10T00:00:00+00:00 Time c to_datetime form : 2020-12-01T00:00:00+00:00 Comment More infoAdvertise with us Next Article Ruby | Time to_datetime() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Time-class Similar Reads Ruby | DateTime to_time() function 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() me 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 Ruby | DateTime sec() function DateTime#sec() : sec() is a DateTime class method which returns the seconds of the DateTime object. Syntax: DateTime.sec() Parameter: DateTime values Return: seconds of the DateTime object. Example #1 : Ruby # Ruby code for DateTime.sec() method # loading library require 'date' # declaring DateTime 2 min read Ruby | Time asctime() function The asctime() is an inbuilt method in Ruby returns a canonical string representation of time. Syntax: time.asctime() Parameters: The function accepts no parameter Return Value: It returns a canonical string representation of time. Example 1: CPP # Ruby code for asctime() method # Include Time requir 1 min read Ruby | Time ctime() function The ctime() is an inbuilt method in Ruby returns a canonical string representation of time. Syntax: time.ctime() Parameters: The function accepts no parameter Return Value: It returns a canonical string representation of time. Example 1: CPP # Ruby code for ctime() method # Include Time require 'tim 1 min read Ruby | DateTime second() function DateTime#second() is a DateTime class method which returns the second value of the DateTime object. Syntax: DateTime.second() Parameter: DateTime values Return: the second value of the DateTime object. Example #1 : Ruby # Ruby code for DateTime.second() method # loading library require 'date' # decl 2 min read Like