Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Creating a date with Ruby (See related posts)

This example creates a date variable which represents 22nd March 2008.
  require 'date'
  d1 = Date.new(y=2008,m=3,d=22)
  puts d1

output: 2008-03-22

Comments on this post

Caius posts on Mar 27, 2008 at 10:19
You can also use Date#parse to turn a string into a date object.

require 'date'
d1 = Date.new(y=2008,m=3,d=22)
d2 = Date.parse("22 march 2008")
puts d1 # => 2008-03-22
puts d2 # => 2008-03-22

You need to create an account or log in to post comments to this site.


Click here to browse all 4829 code snippets

Related Posts