Skip to content

Instantly share code, notes, and snippets.

@capripot
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capripot/82cbd7041865a3180320 to your computer and use it in GitHub Desktop.
Save capripot/82cbd7041865a3180320 to your computer and use it in GitHub Desktop.
Date and Time convenient formatting shortcuts on DateTime, Time and Date objects in Ruby
class Time
def pretty(format = nil)
return unless self
if format == :d
self.strftime("%b. %-d, %Y")
elsif format == :t
self.strftime("%l:M%P")
elsif format == :db
self.strftime("%Y-%m-%d %H:%M:%S")
else
self.strftime("%b. %-d, %Y %l:%M%P")
end
end
end
class Date
def pretty(format = nil)
return unless self
if format == :db
self.strftime("%Y-%m-%d")
else
self.strftime("%b. %-d, %Y")
end
end
end
class DateTime
def pretty(format = nil)
return unless self
if format == :d
self.strftime("%b. %-d, %Y")
elsif format == :t
self.strftime("%l:m%P")
elsif format == :db
self.strftime("%Y-%m-%d %H:%M:%S")
else
self.strftime("%b. %-d, %Y %l:%M%P")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment