ruby time & date

Post on 20-Jan-2017

69 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ruby Time &

Date

The Time Class

Can store a date (day/hour/year) and a time (hour/minute/second)

Get current time using Time.new or Time.now

Time Formatting

Use the strftime method to format time output in any way you want.

time.strftime("%d/%m/%Y") # "05/12/2015"time.strftime("%k:%M") # "17:48"time.strftime("Today is %A") # "Today is Sunday"

Time Arithmetic

You can add a number of seconds to a time object.

For example, add ten seconds:

time = Time.new + 10

Time Parsing

You can use the strptime method to parse dates.

Date.strptime("3 of September", "%d of %B")# 2015-09-03

Rails Time

Rails adds a number of Time classes via ActiveSupport.

For example:3.days.ago

Which returns a ActiveSupport::TimeWithZone object.

Thank You

Check the blog post for the full details:

http://www.blackbytes.info/2015/12/ruby-time/

top related