Initializing a DateTime object the best way

W

Wes Gamble

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
 
W

Wes Gamble

Wes said:
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
 
K

khaines

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 Haines
 
W

Wes Gamble

Wes said:
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
 
S

S Wayne

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

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top