Ruby | DateTime strptime() function Last Updated : 09 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 code for DateTime.strptime() method # loading library require 'date' # declaring DateTime value date_a = DateTime.strptime('10-02-2015 05:05:06 PM', '%d-%m-%Y %I:%M:%S %p') # declaring DateTime value date_b = DateTime.strptime('2018 02 3 07 10 09 +7', '%Y %U %w %H %M %S %z') # strptime method puts "DateTime strptime form : #{date_a}\n\n" puts "DateTime strptime form : #{date_b}\n\n" Output : DateTime strptime form : 2015-02-10T17:05:06+00:00 DateTime strptime form : 2018-01-17T07:10:09+07:00 Example #2 : Ruby # Ruby code for DateTime.strptime() method # Ruby code for DateTime.strptime() method # loading library require 'date' # declaring DateTime value date_a = DateTime.strptime('2010 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z') # declaring DateTime value date_b = DateTime.strptime('-1', '%s') # strptime method puts "DateTime strptime form : #{date_a}\n\n" puts "DateTime strptime form : #{date_b}\n\n" Output : DateTime strptime form : 2010-01-30T04:05:06+07:00 DateTime strptime form : 1969-12-31T23:59:59+00:00 Comment More infoAdvertise with us Next Article Ruby | DateTime to_s() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby DateTime-class Similar Reads 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_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 | 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 | 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 | 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 | 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 Like