Last active
August 29, 2015 14:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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