Ruby

« Go back to Cheatsheets

Time

1
2
3
4
5
6
# current time
Time.now # 2009-06-24 12:39:54 +0900

# Return Time string with certain format - dd-Mon-yyyy HH:MM am/pm
"%d-%b-%Y %-l:%M%P" # 27-Jan-2016 7:19am
"%d/%m/%Y %-l:%M%P" # 27/01/2016 7:19am

More about Time API - Link

Changing the format of Date & Time

1
2
3
4
5
<%= Time.now.strftime("%m/%d/%Y") %>
<%= DateTime.now.strftime("%m/%d/%Y") %>

<%= DateTime.now.strftime("%e-%b-%Y %m:%M %p %Z") %>
# outputs - 12-Feb-2016 07:05 PM IST

In case if you want custom format use Strftime

Comments