Ruby | DateTime parse() function

DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object.

Syntax: DateTime.parse()

Parameter: DateTime values

Return: given representation of date and time, and creates a DateTime object.

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for DateTime.parse() method
  
# loading library
require 'date'
  
# declaring DateTime value
date_a = DateTime.parse('20180403T030908+0700')
  
# declaring DateTime value
date_b = DateTime.parse('2019-04-03T04:04:02+08:00')
  
  
  
#  parse method
puts "DateTime parse form : #{date_a}\n\n"
  
puts "DateTime parse form : #{date_b}\n\n"

chevron_right


Output :

DateTime parse form : 2018-04-03T03:09:08+07:00

DateTime parse form : 2019-04-03T04:04:02+08:00

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for DateTime.parse() method
  
# loading library
require 'date'
  
# declaring DateTime value
date_a = DateTime.parse('3rd Feb 2001 04:05:06 PM')
  
# declaring DateTime value
date_b = DateTime.parse('10 Aug 2018 04:10:06+04:30')
  
# declaring DateTime value
date_c = DateTime.parse('20010203T040506+03:00')
  
  
#  parse method
puts "DateTime parse form : #{date_a}\n\n"
  
puts "DateTime parse form : #{date_b}\n\n"
  
puts "DateTime parse form : #{date_c}\n\n"

chevron_right


Output :

DateTime parse form : 2001-02-03T16:05:06+00:00

DateTime parse form : 2018-08-10T04:10:06+04:30

DateTime parse form : 2001-02-03T04:05:06+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.