Learn and Learn

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

Ruby – _strptime() Function – Examples & Explanation

_strptime() method exists in the standard library of Ruby programming. The _strptime() method describes the given representation of date and time and with the given templates and returns the described elements in Ruby programming language. To find out the parse of the any given date in Ruby, use _strptime() method.

_strptime() Method in Ruby

date._strptime() method exists in Standard Date Library of Ruby. The purpose of this method is to describes the given representation of date and time with the given templates and returns the described elements. The _strptime() is not supported for the indicated flags and width.

 

Syntax of the _strptime() Method in Ruby

In date, the syntax of _strptime() method is

 

Parameters of _strptime() Method in Ruby

x – Where x is any valid date object. This parameter is required.
Note: If the x parameter is not a date object, _strptime() method will return an error.

 

Return Value of _strptime()

_strptime() method will return the given representation of date and time with the given templates and returns the described elements.

 

Explain Date with _strptime Method Example 1


Output
{:year=>2011, :mon=>12, :mday=>23}

 

Explain Date with _strptime Method Example 2


Output:
{:mday=>23, :mon=>12, :year=>2011}

 

Explain Date and Time with _strptime Method Example 3


Output
{:mon=>9, :mday=>18, :year=>2012, :hour=>7, :min=>0}

 

_strptime() Format Directives

Following are some of the mostly used format directives in _strptime():

#FormatDescription
1%AThe uppercase (A) means the full week day name like “MONDAY”
2%aThe lowercase (a) means the abbreviated name for a week day like “Mon”
3%BThe uppercase (B) means the full month name like “MARCH”
4%bThe lowercase (b) means the abbreviated name for a month like “Mar”
5%DIt means date like m/d/y.
6%dIt means any day of the year like (01 to 31)
7%MIt means clock time like (00 to 59)
8%mIt means the number of a month like (01 to 12)
9%SIt means second of the minutes like (00 to 59)
10%sIt means Number of seconds like since 2010-01-01 00:00:00 GMT.
11%YIt means the year of the century
12%yIt means year as (2009 become 09)

 

LearnAndLearn.com © 2018 - All rights reserved