ruby time & date
TRANSCRIPT
![Page 1: Ruby Time & Date](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/1.jpg)
Ruby Time &
Date
![Page 2: Ruby Time & Date](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/2.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/3.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/5.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/6.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022020301/5881a0651a28ab1a398b515f/html5/thumbnails/7.jpg)
Thank You
Check the blog post for the full details:
http://www.blackbytes.info/2015/12/ruby-time/