A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.ruby-lang.org/en/3.4/strftime_formatting_rdoc.html below:

strftime_formatting - Documentation for Ruby 3.4

Formats for Dates and Times

Several Ruby time-related classes have instance method strftime, which returns a formatted string representing all or part of a date or time:

Each of these methods takes optional argument format, which has zero or more embedded format specifications (see below).

Each of these methods returns the string resulting from replacing each format specification embedded in format with a string form of one or more parts of the date or time.

A simple example:

Time.now.strftime('%H:%M:%S') 

A format specification has the form:

%[flags][width]conversion

It consists of:

Except for the leading percent character, the only required part is the conversion specifier, so we begin with that.

Conversion Specifiers Date (Year, Month, Day) Time (Hour, Minute, Second, Subsecond) Timezone Weekday Week Number Week Dates

See ISO 8601 week dates.

t0 = Time.new(2023, 1, 1) 
t1 = Time.new(2024, 1, 1) 
Literals Shorthand Conversion Specifiers

Each shorthand specifier here is shown with its corresponding longhand specifier.

Flags

Flags may affect certain formatting specifications.

Multiple flags may be given with a single conversion specified; order does not matter.

Padding Flags Casing Flags Timezone Flags Width Specifiers

The integer width specifier gives a minimum width for the returned string:

Time.new(2002).strftime('%Y')       
Time.new(2002).strftime('%10Y')     
Time.new(2002, 12).strftime('%B')   
Time.new(2002, 12).strftime('%10B') 
Time.new(2002, 12).strftime('%3B')  
Specialized Format Strings

Here are a few specialized format strings, each based on an external standard.

HTTP Format

The HTTP date format is based on RFC 2616, and treats dates in the format '%a, %d %b %Y %T GMT':

d = Date.new(2001, 2, 3) 

httpdate = d.httpdate    

Date.httpdate(httpdate)  

Date._httpdate(httpdate)

RFC 3339 Format

The RFC 3339 date format is based on RFC 3339:

d = Date.new(2001, 2, 3) 

rfc3339 = d.rfc3339      

Date.rfc3339(rfc3339)    

Date._rfc3339(rfc3339)

RFC 2822 Format

The RFC 2822 date format is based on RFC 2822, and treats dates in the format '%a, %-d %b %Y %T %z']:

d = Date.new(2001, 2, 3) 

rfc2822 = d.rfc2822      

Date.rfc2822(rfc2822)    

Date._rfc2822(rfc2822)

JIS X 0301 Format

The JIS X 0301 format includes the Japanese era name, and treats dates in the format '%Y-%m-%d' with the first letter of the romanized era name prefixed:

d = Date.new(2001, 2, 3) 

jisx0301 = d.jisx0301    

Date.jisx0301(jisx0301)  

Date._jisx0301(jisx0301) 
ISO 8601 Format Specifications

This section shows format specifications that are compatible with ISO 8601. Details for various formats may be seen at the links.

Examples in this section assume:

t = Time.now 
Dates

See ISO 8601 dates.

Times

See ISO 8601 times.

Combined Date and Time

See ISO 8601 Combined date and time representations.

An ISO 8601 combined date and time representation may be any ISO 8601 date and any ISO 8601 time, separated by the letter T.

For the relevant strftime formats, see Dates and Times above.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4