Hello,
I've put together a new date formatter gem / library [1] that lets you
format dates e.g. "January 02, 2006"
using an example as a format string e.g "January 02, 2006"
instead of the classic strftime format specifier e.g. "%B %d, %Y". The
date by example adds:
- String#to_strfime
- Date#format
- Time#format
- DateTime#format
- NilClass#format
to the built-in classes.
Example - The new `String#to_strftime` method auto-builds the
`strftime` format string from an example date:
require 'date/formatter'
p 'January 02, 2006'.to_strftime #=> "%B %d, %Y"
p 'Mon, Jan 02'.to_strftime #=> "%a, %b %d"
p '2 Jan 2006'.to_strftime #=> "%-d %b %Y"
p 'Monday, January 2, 2006'.to_strftime #=> "%A, %B %-d, %Y"
p 'Mon, Jan 02 3:00'.to_strftime #=> "%a, %b %d %-H:%M"
p '2 Mon 2006 03:00'.to_strftime #=> "%-d %b %Y %H:%M"
Example - The new `Date#format` method formats the date like the
passed in example:
date = Date.today ## test run on 2020-02-09
p date.format( 'January 02, 2006' ) #=> "February 09, 2020"
p date.format( 'Mon, Jan 02' ) #=> "Sun, Feb 09"
p date.format( '2 Jan 2006' ) #=> "9 Feb 2020"
p date.format( 'Monday, January 2, 2006' ) #=> "Sunday, February 9, 2020"
and so on. Happy date and time formatting with ruby. Cheers. Prost.
[1] https://github.com/feedreader/pluto/tree/master/date-formatter