Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to use the full strftime cheat sheet in Ruby

1 Answer

0 votes
=begin

Full Ruby strftime Cheatsheet:

Date (Year, Month, Day)
 %Y  — Year with century (e.g., 2026)
 %y  — Year without century (00–99)
 %C  — Century (year / 100)
 %m  — Month (01–12), zero‑padded
 %_m  — Month, space‑padded
 %-m  — Month, no padding
 %B  — Full month name (January)
 %b / %h  — Abbreviated month name (Jan)
 %d  — Day of month (01–31), zero‑padded
 %-d  — Day, no padding
 %e  — Day, space‑padded
 %j  — Day of year (001–366)

Time (Hour, Minute, Second, Subsecond)
 %H  — Hour (00–23), zero‑padded
 %k  — Hour (0–23), space‑padded
 %I  — Hour (01–12), zero‑padded
 %l  — Hour (1–12), space‑padded
 %M  — Minute (00–59)
 %S  — Second (00–60, leap second)
 %L  — Milliseconds (000–999)
 %N  — Nanoseconds (9 digits)
    %3N → milliseconds
    %6N → microseconds
    %9N → nanoseconds
 %p  — AM/PM (uppercase)
 %P  — am/pm (lowercase)

Time Zone
 %z  — Offset (+0900)
 %:z  — Offset with colon (+09:00)
 %::z  — Offset with seconds (+09:00:00)
 %Z  — Time zone abbreviation (OS‑dependent)

Weekday
 %A  — Full weekday name (Sunday)
 %a  — Abbreviated weekday (Sun)
 %u  — Weekday (1=Mon … 7=Sun)
 %w  — Weekday (0=Sun … 6=Sat)

ISO Week‑Based Year
 %G  — ISO week‑based year
 %g  — Last 2 digits of ISO year
 %V  — ISO week number (01–53)

Seconds Since Epoch
 %s  — Unix timestamp (seconds)
 %Q  — Unix timestamp (milliseconds)

Literal Characters
 %n  — Newline
 %t  — Tab
 %%  — Literal %

Composite / Convenience Formats
 %c  — Preferred date+time (%a %b %e %T %Y)
 %D  — %m/%d/%y
 %F  — ISO 8601 date (%Y-%m-%d)
 %v  — VMS date (%e-%b-%Y)
 %x  — Locale date
 %X  — Locale time
 %r  — 12‑hour time (%I:%M:%S %p)
 %R  — 24‑hour time (%H:%M)
 %T  — 24‑hour time (%H:%M:%S)
 %+  — Date with time zone (%a %b %e %H:%M:%S %Z %Y)

Padding & Case Flags
 -  — No padding
 _  — Space padding
 0  — Zero padding
 ^  — Uppercase result
 #  — Change case
 :  — Use colons for %z

=end


require "time"

now = Time.now

def show(label, pattern, now)
  puts "#{label.ljust(30)} #{pattern.ljust(12)} → #{now.strftime(pattern)}"
end

puts "=== Ruby strftime Full Cheatsheet Demo ==="
puts "Current time: #{now}"
puts "-" * 60

# --- YEAR ---
show("Year (4 digits)", "%Y", now)
show("Year (2 digits)", "%y", now)
show("Century", "%C", now)

# --- MONTH ---
show("Month (01-12)", "%m", now)
show("Month (1-12, space)", "%_m", now)
show("Month (1-12, no pad)", "%-m", now)
show("Month name (short)", "%b", now)
show("Month name (full)", "%B", now)

# --- DAY ---
show("Day (01-31)", "%d", now)
show("Day (1-31, space)", "%e", now)
show("Day of year (001-366)", "%j", now)

# --- WEEKDAY ---
show("Weekday (0=Sun)", "%w", now)
show("Weekday (1=Mon)", "%u", now)
show("Weekday name (short)", "%a", now)
show("Weekday name (full)", "%A", now)

# --- TIME ---
show("Hour (00-23)", "%H", now)
show("Hour (0-23, space)", "%k", now)
show("Hour (01-12)", "%I", now)
show("Hour (1-12, space)", "%l", now)
show("Minute (00-59)", "%M", now)
show("Second (00-60)", "%S", now)
show("Millisecond", "%L", now)
show("Nanoseconds", "%N", now)
show("AM/PM", "%p", now)
show("am/pm", "%P", now)

# --- TIME ZONE ---
show("Timezone abbrev", "%Z", now)
show("Offset (+0900)", "%z", now)
show("Offset (+09:00)", "%:z", now)
show("Offset (+09:00:00)", "%::z", now)

# --- ISO WEEK YEAR ---
show("ISO week year", "%G", now)
show("ISO week year (2-digit)", "%g", now)
show("ISO week number", "%V", now)

# --- UNIX TIME ---
show("Unix timestamp (sec)", "%s", now)
show("Unix timestamp (ms)", "%Q", now)

# --- COMPOSITE ---
show("Preferred date+time", "%c", now)
show("mm/dd/yy", "%D", now)
show("ISO date", "%F", now)
show("VMS date", "%v", now)
show("Locale date", "%x", now)
show("Locale time", "%X", now)
show("12-hour time", "%r", now)
show("24-hour time (HH:MM)", "%R", now)
show("24-hour time (HH:MM:SS)", "%T", now)
show("Full date with TZ", "%+", now)

# --- LITERALS ---
show("Literal %", "%%", now)
show("Newline", "%n", now)
show("Tab", "%t", now)



=begin
run:

=== Ruby strftime Full Cheatsheet Demo ===
Current time: 2026-05-21 05:23:28 +0000
------------------------------------------------------------
Year (4 digits)                %Y           → 2026
Year (2 digits)                %y           → 26
Century                        %C           → 20
Month (01-12)                  %m           → 05
Month (1-12, space)            %_m          →  5
Month (1-12, no pad)           %-m          → 5
Month name (short)             %b           → May
Month name (full)              %B           → May
Day (01-31)                    %d           → 21
Day (1-31, space)              %e           → 21
Day of year (001-366)          %j           → 141
Weekday (0=Sun)                %w           → 4
Weekday (1=Mon)                %u           → 4
Weekday name (short)           %a           → Thu
Weekday name (full)            %A           → Thursday
Hour (00-23)                   %H           → 05
Hour (0-23, space)             %k           →  5
Hour (01-12)                   %I           → 05
Hour (1-12, space)             %l           →  5
Minute (00-59)                 %M           → 23
Second (00-60)                 %S           → 28
Millisecond                    %L           → 171
Nanoseconds                    %N           → 171670708
AM/PM                          %p           → AM
am/pm                          %P           → am
Timezone abbrev                %Z           → UTC
Offset (+0900)                 %z           → +0000
Offset (+09:00)                %:z          → +00:00
Offset (+09:00:00)             %::z         → +00:00:00
ISO week year                  %G           → 2026
ISO week year (2-digit)        %g           → 26
ISO week number                %V           → 21
Unix timestamp (sec)           %s           → 1779341008
Unix timestamp (ms)            %Q           → %Q
Preferred date+time            %c           → Thu May 21 05:23:28 2026
mm/dd/yy                       %D           → 05/21/26
ISO date                       %F           → 2026-05-21
VMS date                       %v           → 21-MAY-2026
Locale date                    %x           → 05/21/26
Locale time                    %X           → 05:23:28
12-hour time                   %r           → 05:23:28 AM
24-hour time (HH:MM)           %R           → 05:23
24-hour time (HH:MM:SS)        %T           → 05:23:28
Full date with TZ              %+           → %+
Literal %                      %%           → %
Newline                        %n           → 
Tab                            %t           → 	

=end

 



answered May 21 by avibootz
...