Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A date class for storing partial dates - for example when a month or day are unknown, or optional.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 benchmarks
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .yardopts
Octocat-spinner-32 ChangeLog.markdown
Octocat-spinner-32 Guardfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 partial-date.gemspec



A simple date class that can be used to store partial date values in a single column/attribute. An example use case would include an archive, or catalogue entry where the complete date is unknown. Year is optional and can be a negative value. Month and day are optional, but month must be set before day.


PartialDate::Date uses a 30 bit register as the backing store for date instances, and bit fiddling to get or set year, month and day values. As such it will perform well in a loop or collection of date objects.

Use date.value to get or set an Integer value that can be used to rehydrate a date object, or save the date value to a persistence store in a readable Integer form e.g. 20121201 for 2012 December 01.

PartialDate::Date#to_s has the following built-in formats:

d.to_s           :default => "%Y-%m-%d"  
d.to_s :short    :short => "%d %m %Y"    
d.to_s :medium   :medium => "%d %b %Y"   
d.to_s :long     :long => "%d %B %Y"     
d_to_s :number   :number => "%Y%m%d"     

Custom formatters can be specified using the following:

%Y - Year with century (can be negative, 4 digits at least)
            -0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
%B - The full month name ('January')
%b - The abbreviated month name ('Jan')
%d - Day of the month, zero-padded (01..31)
%e - Day of the month, blank-padded ( 1..31)


require 'partial-date' 

# Default initializer 
date =
# => 
# => 0

# Initialize from a block of integers
date = {|d| d.year = 2012; d.month = 01}
# => 2012-01
# => 20120100

# Initialize from a block of strings
date = {|d| d.year = "2012"; d.month = "01"}
# => 2012-01
# => 20120100
date.to_s :medium
# => Jan 2012

# Initialize from the class load method - for rehydrating a date instance from a stored integer date value.
date = PartialDate::Date.load 20121201
# => 2012-12-01
# => 2012
# => 12
# => 1
date.to_s :long
# => 01 December 2012


$ gem install partial-date


  • PartialDate::Date#parse method for construction from strings.
  • I18n support.


Copyright (c) 2012 Anthony Bouch

See {file:LICENSE.txt} for details.

Something went wrong with that request. Please try again.