# change integer into date

Discussion in 'Ruby' started by Pen Ttt, Dec 31, 2010.

1. ### Pen TttGuest

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

--
Posted via http://www.ruby-forum.com/.

Pen Ttt, Dec 31, 2010

2. ### Nathan ClarkGuest

> require 'date'
> =C2=A0biaozun=3DDate.new(1970,1,1)
> =C2=A0puts biaozun+1262222200/86400
> =3D>2009-12-31
> i have change =C2=A01262222200 into date,2009-12-31
> 1.can i change =C2=A02009-12-31 =C2=A0into =C2=A01262222200?
> 2.is there better way to change =C2=A01262222200 into date,2009-12-31

Methods for dealing with Unix times are in the Time class, rather than
the Date class.

irb> time =3D Time.at(1262222200)
=3D> 2009-12-30 20:16:40 -0500
irb> time.to_i
=3D> 1262222200
irb> time =3D Time.new(2010, 12, 31)
=3D> 2010-12-31 00:00:00 -0500
irb> time.to_i
=3D> 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).

Nathan Clark, Dec 31, 2010

3. ### Colin BartlettGuest

These might help?

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.

IRB in Ruby 1.9.1

require "date"
biaozun = Date.new( 1970, 1, 1 ) #=> #<Date: 1970-01-01 ...>
duration_secs = 1_262_222_200 #=> 1262222200
duration_days_rational = duration_secs.quo( 86400 ) #=> (6311111/432)
duration_days_integer = duration_secs.div( 86400 ) #=> 14609
duration_days_integer2 = duration_secs / 86400 #=> 14609
date2 = biaozun + duration_days_integer #=> #<Date: 2009-12-31 ...>
exit
unix_date_zero_jd = biaozun.jd #=> 2440588

d2jd = date2.jd #=> 2455197
dur2_secs = (d2jd - 2440588) * 86400 #=> 1262217600
ut = Time.at( dur2_secs ) #=> 2009-12-31 00:00:00 +0000

dt = DateTime.new( 2010, 12, 31, 13, 42 )
#=> #<DateTime: 2010-12-31T13:42:00+00:00 ...>

t = dt.to_time #=> 2010-12-31 13:42:00 +0000
t2 = date2.to_time #=> 2009-12-31 00:00:00 +0000

Colin Bartlett, Dec 31, 2010
4. ### Pen TttGuest

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

it's wrong for the expression: Time.new(2010, 12, 31)

--
Posted via http://www.ruby-forum.com/.

Pen Ttt, Jan 1, 2011
5. ### Skye Shaw!@#\$Guest

On Dec 31 2010, 5:42 pm, Pen Ttt <> 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
>

Skyes-MacBook-Pro-15erl sshaw\$ ruby19 -ve'p Time.new 2011,1,1'
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.2.0]
2011-01-01 00:00:00 -0800
Skyes-MacBook-Pro-15erl sshaw\$ ruby -ve'p Time.local 2011,1,1'
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
Sat Jan 01 00:00:00 -0800 2011

Skye Shaw!@#\$, Jan 1, 2011