Tiny DSL for idiomatic date parsing and formatting.
require "yymmdd"
include YYMMDD
puts yy/mm # 14/08 (i.e., today's date)
puts yyyy/mm # 2014/08
date = ymd(411207) # Date.new(1941, 12, 7)
date = Date.today
puts yyyy.mm.dd(date) # 2014.08.09
puts dd/mm/yy(date) # 08/09/14
puts ymd(date) # 1489
puts yymmdd(date) # 140809
date = yyyy.mm.dd("1941.12.07") # Date.new(1941, 12, 7)
date = mm.dd.yy("11.22.63") # Date.new(1963, 11, 22)
date = mm/dd/yy("11/21/99") # ...
date = mm/dd/yyyy("11/21/1999")
date = mm-dd-yyyy("11-21-1999")
date = m-d-y("11-21-99")
gem install yymmdd
gem "yymmdd"
All functions are module_functions
so you must include YYMMDD to use them.
When given a String it will attempt to parse it as the specified format and return a Date.
When given a Date it will return a String in the specified format.
An ArgumentError is raised if the date can't be parsed or formatted.
With no arguments it will return an instance of a String-like object (it overrides to_s and to_str) representing
today's date in the specified format. In the most common cases you can treat it like a String:
date = yyyy/mm/dd
puts "Today's date: #{date}"
text = ["Dates: ", yy/mm, yyyy/mm].join(", ")
text = "A great date: " << date
But in some instances you'll have to expilictly call to_s:
printf "Today's date: %s\n", date.to_s
All the heavy lifting is done by Date#strftime and Date.strptime.
The table below lists the available format specifiers. All of these can be separated by one of the supported
delimiters: "/", ".", or "-".
| Name | Format |
|---|---|
d | day of the year, no 0 padding |
dd | day of the year |
m | day of the month, no 0 padding |
mm | day of the month |
y | 2 digit year |
yy | 2 digit year |
yyyy | 4 digit year |
There are also combined, delimiterless functions for all combinations of the above, e.g., ymd, mdy, yymmdd, etc...
Due to operator precedence you can't mix delimiters.
Skye Shaw [sshaw AT gmail.com]
Released under the MIT License: www.opensource.org/licenses/MIT
