The parse
method describes the given representation of date and time and returns the described elements. To find out the parse of the any given date in Ruby, date.parse()
method is used.
date.parse()
Method in Ruby
date.parse()
method exists in Standard Date Library of Ruby. The purpose of this method is to provide an explanation for any given date.
Syntax of the parse
Method in Ruby
In date, the syntax of parse method is:
date.parse(x)
Parameter of date.parse()
Method in Ruby
The parameter x
is any valid date string that could be converted into a date object. This parameter is required.Note: If the x
parameter is not a date object, date.parse()
method will return an error.
Return Value of date.parse()
date.parse
method will return the parsed elements of any given date.
Parse any Date in Ruby
# import date library require 'date' date = Date.parse('2015-09-13') puts date
Output
2015-09-13
Various Formats of date.parse()
Method in Ruby
# import date library require 'date' date = Date.parse('2011-December-23') puts date
Output
2011-12-23
# import date library require 'date' date = Date.parse('20150823') puts date
Output
2015-08-23
Explaining Date and Time with date.parse()
Method.
# import date library require 'date' date = DateTime.parse('20150823 12: 44: 05') puts date
Output
2015-08-23T12:44:05+00:00
Adding Days in Current Date with date.parse()
Method
Adding days in the current date. # import date library require 'date' date = Date.parse('Apr 26, 2015 04:20 GMT').next_day(5) puts date
Output
2015-05-01
Subtracting Days from Current Date with date.parse()
Method
Subtracting days from the current date.
# import date library require 'date' date = Date.parse('Apr 26, 2015 04:20 GMT').prev_day(5) puts date
Output
2015-04-21