Open In App

Ruby | DateTime strptime() function

Last Updated : 09 Jan, 2020
Comments
Improve
Suggest changes
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


Next Article

Similar Reads