Ruby Time & Date

7
Ruby Time & Date

Transcript of Ruby Time & Date

Page 1: Ruby Time & Date

Ruby Time &

Date

Page 2: 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

Page 3: Ruby Time & Date

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"

Page 4: Ruby Time & Date

Time Arithmetic

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

For example, add ten seconds:

time = Time.new + 10

Page 5: Ruby Time & Date

Time Parsing

You can use the strptime method to parse dates.

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

Page 6: Ruby Time & Date

Rails Time

Rails adds a number of Time classes via ActiveSupport.

For example:3.days.ago

Which returns a ActiveSupport::TimeWithZone object.

Page 7: Ruby Time & Date

Thank You

Check the blog post for the full details:

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