require 'date'
biaozun=Date.new(1970,1,1)
puts biaozun+1262222200/86400
=>2009-12-31
i have change 1262222200 into date,2009-12-31
1.can i change 2009-12-31 into 1262222200? 2.is there better way to change 1262222200 into date,2009-12-31
require 'date'
biaozun=Date.new(1970,1,1)
puts biaozun+1262222200/86400
=>2009-12-31
i have change 1262222200 into date,2009-12-31
1.can i change 2009-12-31 into 1262222200?
2.is there better way to change 1262222200 into date,2009-12-31
Methods for dealing with Unix times are in the Time class, rather than
the Date class.
time = Time.at(1262222200)
=> 2009-12-30 20:16:40 -0500
time.to_i
=> 1262222200
time = Time.new(2010, 12, 31)
=> 2010-12-31 00:00:00 -0500
time.to_i
=> 1293771600
Unfortunately, there doesn't seem to be any good way to convert from
Date to Time, other than
Time.new(date_object.to_s).
Date#jd gives you the Julian Day Number of a date,
and the Julian Day Number of 1970-01-01 (Gregorian) is 2440588,
so you can do things like the following.
There is also the DateTime class, which I have not used until this post.
"Date" also adds Time#to_date and Time#to_datetime, but I haven't tried those.
in my irb,
pt@pt:~$ ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
pt@pt:~$ irb
irb(main):001:0> time = Time.new(2010, 12, 31)
ArgumentError: wrong number of arguments (3 for 0)
from (irb):1:in `initialize'
from (irb):1:in `new'
from (irb):1
it's wrong for the expression: Time.new(2010, 12, 31)
On Dec 31 2010, 5:42 pm, Pen Ttt <myocean...@yahoo.cn> wrote:
in my irb,
pt@pt:~$ ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
pt@pt:~$ irb
irb(main):001:0> time = Time.new(2010, 12, 31)
ArgumentError: wrong number of arguments (3 for 0)
from (irb):1:in `initialize'
from (irb):1:in `new'
from (irb):1
from :0