Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Ruby – Get Valid Date – valid_date? Method of Date Library – Examples & Explanation

The valid_date? method gives the output true if the given calendar date in a code is a valid date otherwise gives false in Ruby language. To get the valid date use valid_date?() method.

valid_date?() Method in Ruby

valid_date?() method exists in Standard Date Library of Ruby. The purpose of this method is to give the valid date of a calendar year from the given code.

 

Syntax of the valid_date?() Method in Ruby

In date, the syntax of valid method is:

Note: If the x parameter is not a valid date or zero the valid_date?() method will return an error.

 

Return Value of valid_date?() Method

valid_date?() method will return true if the given date of a calendar is a valid date or else gives false.

 

Get valid date in Ruby


Output
true

 

How to set valid_date?() Format in Ruby?


Output
true
false
false
false

NOTE: You can set different date formats to get you desired output like (%d %m %Y), (%m %d %Y) and (%Y %d %m).

 

How to validate date in Ruby?


Output
false
true
false

 

Parse abbreviated date Directly in Ruby


Output
{:mon=>3, :mday=>25}

Output
2017-03-25

Output
“2017-03-25”

 

Parse a date string in Ruby


Output
2015-05-15

Output
2015-05-15T13:25:00+00:00

Output
{:year=>2015, :mon=>5, :mday=>15, :hour=>13, :min=>25}

 

LearnAndLearn.com © 2018 - All rights reserved