# Time, Date and DateTime

**URL:** <https://www.ruby-forum.com/t/time-date-and-datetime/184288>\
**Category:** Ruby\
**Created:** [February 28, 2010, 7:02am UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288 "2010-02-28T07:02:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [February 28, 2010, 7:02am UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288/1 "2010-02-28T07:02:52Z")

</div>

Hi,

I am trying to understand the key differences between Time, Date and  
DateTime classes.

When should one use one over the other? Does anyone know of a good post  
that explains each of these classes and post on how to do basic time,  
date arithmetic or conversion between each?

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 1, 2010, 12:47am UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288/2 "2010-03-01T00:47:03Z")

</div>

Rajinder Y. wrote:

> Hi,
> 
> I am trying to understand the key differences between Time, Date and  
> DateTime classes.
> 
> When should one use one over the other? Does anyone know of a good post  
> that explains each of these classes and post on how to do basic time,  
> date arithmetic or conversion between each?

Note: I am by no means an expert on this.

Time provides you with a date and a time and is fine if you are sure the  
date ranges from the year 1970 to 2037.  
It’s excellent for timestamps and such. Essentially, it’s just the  
number of seconds elapsed since 1970-1-1. So if the provided methods  
don’t suit you, you have to calculate in seconds, using #to\_i and #at .

Date is a heavyweight, allowing for Julian or Gregorian calendars,  
specifying the reform dates. You don’t have to worry about date ranges  
(unless you need dates like -5000-1-1). Calculating is superior to Time  
objects.

DateTime is the same as a Date with hours, minutes and seconds.

All three can be enhanced by require-ing active\_support.

The creation of a Date object is about ten times slower than the  
creation of a time object (on my PC).

In Ruby 1.9 conversion is simple: #to\_date, #to\_time and #to\_datetime.  
For versions earlier: google, or copy the 1.9 methods from the  
documentation (untested).

hth,

Siep

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 3, 2010, 4:19am UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288/3 "2010-03-03T04:19:46Z")

</div>

Siep K. wrote:

> Note: I am by no means an expert on this.  
> objects.  
> documentation (untested).
> 
> hth,
> 
> Siep

Hi Siep,

thank you, this is the kind of background (comparison) info I was  
looking for. The active\_support lib really makes date calculation easy.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 4, 2010, 2:10am UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288/4 "2010-03-04T02:10:59Z")

</div>

On Thu, Mar 4, 2010 at 12:34 AM, Benoit D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And it’s probably preferred to Date, because you can use it without any  
> require.

very nice. thanks for the info, benoit.  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 3, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/time-date-and-datetime/184288/5 "2010-03-03T17:35:49Z")

</div>

Just to mention Time in 1.9.2 doesn’t get limits:  
"Ruby 1.9.2 preview 1 has been released.  
[…]

- Time was reimplemented and enhanced. Now Time has no max/min value,  
no  
year 2038 problem."

> Time.new(400000000000).to\_i.class # If still there is a Earth at that Time  
> =\> Bignum  
> Time.new(400000000000).to\_i  
> =\> 12622780739410700400  
> Time.new(-5000).to\_i  
> =\> -219951849600

And it’s probably preferred to Date, because you can use it without any  
require.
