Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Date Methods in Ruby

Date Methods in Ruby

Intro to Date methods in Ruby

Avatar for Marge

Marge

July 25, 2013

More Decks by Marge

Other Decks in Programming

Transcript

  1. There are many ways to express dates: January 1, 2013

    01/01/2013 01-01-13 01 Jan 13 001 Jan 01, 2013 January 1st, 2013 Tuesday, Jan 1, 2013 13.01.01
  2. Dates are everywhere and all businesses need ways to calculate

    dates accurately: • scheduling • billing • taxes • payroll
  3. What’s the difference between strptime and strftime? http://stackoverflow.com/questions/14619494/ruby-strptime-vs-strftime The difference

    between Time and DateTime has to do with implementation. DateTime comes from the Rails world and is an arbitrary date with time of day. It's more of a calendar-based system. DateTime is what you get from a database in Rails. Time is what Ruby has traditionally used. Time is measured as seconds since January 1, 1970 UTC and is time- zone agnostic. If you're working with values where dates are important and you want to know things like the end of the month or what day it'll be six weeks ahead, use DateTime. If you're just measuring elapsed time and don't care about dates, use Time. They're easy to convert between if necessary. Date on the other hand is just a calendar date and doesn't have any associated times. You might want to use these where time is irrelevant. strptime is short for "parse time" strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification. DateTime.parse similar to strptime. Strptime is the legacy parser.
  4. Ruby Gems https://www.ruby-toolbox.com/projects/calendar_date_select A popular and flexible JavaScript DatePicker for

    RubyOnRails (and others) https://github.com/timcharper/calendar_date_select http://rubygems.org/gems/calendar_date_select 186,826 total downloads 20,640 for this version
  5. DateSetter: 1.4.2, 1.4.1 http://rubygems.org/gems/DateSetter Sets a random date in given

    range. Public Class Methods and Instance Methods last modified 2013-07-13 8,430 total downloads !431 for this version