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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

DateTime strptime form : 2015-02-10T17:05:06+00:00

DateTime strptime form : 2018-01-17T07:10:09+07:00

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

DateTime strptime form : 2010-01-30T04:05:06+07:00

DateTime strptime form : 1969-12-31T23:59:59+00:00



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.