Validate a date : Time Extension « Time « Ruby

Home
Ruby
1.ActiveRecord
2.Array
3.CGI
4.Class
5.Collections
6.Database
7.Date
8.Design Patterns
9.Development
10.File Directory
11.GUI
12.Hash
13.Language Basics
14.Method
15.Network
16.Number
17.Rails
18.Range
19.Reflection
20.Statement
21.String
22.Threads
23.Time
24.Tk
25.Unit Test
26.Windows Platform
27.XML
Ruby » Time » Time Extension 




Validate a date


class Time

  def Time.validate(year, month=1, day=1,
                    hour=0, min=0, sec=0, usec=0)
    require "date"

    begin
      d = Date.new(year,month,day)
    rescue
      return nil
    end
    Time.local(year,month,day,hour,min,sec,usec)
  end

end

t1 = Time.validate(2000,11,30)  # Instantiates a valid object
t2 = Time.validate(2000,11,31)  # Returns nil

 














Related examples in the same category
1.Add day ordinal suffix to Time
2.Add step method to Time
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.