Initializing a DateTime object the best way

Wow - dates and times sure are all over the place in Ruby.

I want to initialize a DateTime object to have MDY of 12/30/1899 but
time components matching the current time.

I’ve tried:

DateTime.strptime("%Y-%m-%d %h:%M:%S", “1899-12-30
#{time.hour}:#{time.min}:#{time.sec}”)

but strptime doesn’t seem to like it.

Do I need to use a Time object - where’s the correct string parsing
method for me to use?

Thanks,
Wes

Wes G. wrote:

Wow - dates and times sure are all over the place in Ruby.

I want to initialize a DateTime object to have MDY of 12/30/1899 but
time components matching the current time.

I’ve tried:

DateTime.strptime("%Y-%m-%d %h:%M:%S", “1899-12-30
#{time.hour}:#{time.min}:#{time.sec}”)

but strptime doesn’t seem to like it.

Do I need to use a Time object - where’s the correct string parsing
method for me to use?

Thanks,
Wes

Then I tried

Time.local(1899,“dec”,30,10,0,0)

and 1899 is too far back.

Is there any way that I can create a time object with arbitrary year,
month, and day and current hour, minute, and second?

Thanks,
Wes

On Tue, 18 Jul 2006, Wes G. wrote:

but strptime doesn’t seem to like it.

Do I need to use a Time object - where’s the correct string parsing
method for me to use?

DateTime is the class to use if you need to represent a wide range of
dates. Time only works for a narrow range starting at Jan 1, 1970.

foo = DateTime.new(1899,12,30,8,30,33)
foo.asctime
=> “Sat Dec 30 08:30:33 1899”

Kirk H.

This is an interesting conversation. Can you tell me where this
DateTime class or the civil method comes from?

irb(main):001:0> DateTime.new(1899,12,30,8,30,33)
ArgumentError: wrong number of arguments (6 for 0)
from (irb):1:in `initialize’
from (irb):1

irb(main):002:0> DateTime.civil(1899,12,30,8,30,33)
NoMethodError: undefined method `civil’ for DateTime:Class
from (irb):2

Wes G. wrote:

Wes G. wrote:

Wow - dates and times sure are all over the place in Ruby.

I want to initialize a DateTime object to have MDY of 12/30/1899 but
time components matching the current time.

I’ve tried:

DateTime.strptime("%Y-%m-%d %h:%M:%S", “1899-12-30
#{time.hour}:#{time.min}:#{time.sec}”)

but strptime doesn’t seem to like it.

Do I need to use a Time object - where’s the correct string parsing
method for me to use?

Thanks,
Wes

Then I tried

Time.local(1899,“dec”,30,10,0,0)

and 1899 is too far back.

Is there any way that I can create a time object with arbitrary year,
month, and day and current hour, minute, and second?

Thanks,
Wes

This seems to get me pretty close.

DateTime.civil(1899, 12, 30, time.hour, time.min, time.sec,
DateTime.now.offset())

WG

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs