Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pokka/898b85b3bf0842b6c2e2 to your computer and use it in GitHub Desktop.
Save pokka/898b85b3bf0842b6c2e2 to your computer and use it in GitHub Desktop.
Ruby script to print out strftime conversion format options and outputs
# Function to print strftime results
def print_strftime_formats(a,cur_date)
a.each do |format|
b = "%#{format}"
output = cur_date.strftime(b)
puts "t.strftime('#{b}'), => #{output}"
end
end
a = ('a'..'z').to_a
A = ('A'..'Z').to_a
cur_date = Time.now
# calling and printing strftime results on current date
puts "DateTime: #{cur_date}\n"
puts "\n\nFor a to z"
print_strftime_formats(a,cur_date)
t.strftime('%a'), => Mon
t.strftime('%b'), => Sep
t.strftime('%c'), => Mon Sep 7 11:04:22 2015
t.strftime('%d'), => 07
t.strftime('%e'), => 7
t.strftime('%f'), => %f
t.strftime('%g'), => 15
t.strftime('%h'), => Sep
t.strftime('%i'), => %i
t.strftime('%j'), => 250
t.strftime('%k'), => 11
t.strftime('%l'), => 11
t.strftime('%m'), => 09
t.strftime('%n'), =>
t.strftime('%o'), => %o
t.strftime('%p'), => AM
t.strftime('%q'), => %q
t.strftime('%r'), => 11:04:22 AM
t.strftime('%s'), => 1441595062
t.strftime('%t'), =>
t.strftime('%u'), => 1
t.strftime('%v'), => 7-SEP-2015
t.strftime('%w'), => 1
t.strftime('%x'), => 09/07/15
t.strftime('%y'), => 15
t.strftime('%z'), => +0800
puts "\n\nFor A to Z"
print_strftime_formats(A,cur_date)
t.strftime('%A'), => Monday
t.strftime('%B'), => September
t.strftime('%C'), => 20
t.strftime('%D'), => 09/07/15
t.strftime('%E'), => %E
t.strftime('%F'), => 2015-09-07
t.strftime('%G'), => 2015
t.strftime('%H'), => 11
t.strftime('%I'), => 11
t.strftime('%J'), => %J
t.strftime('%K'), => %K
t.strftime('%L'), => 851
t.strftime('%M'), => 04
t.strftime('%N'), => 851594000
t.strftime('%O'), => %O
t.strftime('%P'), => am
t.strftime('%Q'), => %Q
t.strftime('%R'), => 11:04
t.strftime('%S'), => 22
t.strftime('%T'), => 11:04:22
t.strftime('%U'), => 36
t.strftime('%V'), => 37
t.strftime('%W'), => 36
t.strftime('%X'), => 11:04:22
t.strftime('%Y'), => 2015
t.strftime('%Z'), => CST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment