# Re: modifying the hour in a datetime object already created

**URL:** <https://www.ruby-forum.com/t/re-modifying-the-hour-in-a-datetime-object-already-created/68716>\
**Category:** Ruby\
**Created:** [September 14, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/re-modifying-the-hour-in-a-datetime-object-already-created/68716 "2006-09-14T18:47:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 14, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/re-modifying-the-hour-in-a-datetime-object-already-created/68716/1 "2006-09-14T18:47:01Z")

</div>

From: Josselin [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> but how can I modify the hour, which stays at 16:00:00 ?

You can’t - DateTime objects are immutable. You’d need to create the  
DateTime object with the correct time.

It’s situations like this which motivated me to make my own MutableTime  
class (that internally wraps changing Time objects). I never wrapped it  
up as a gem, but you can view the documentation and download it from:

[http://phrogz.net/RubyLibs/rdoc/files/MutableTime\_rb.html](http://phrogz.net/RubyLibs/rdoc/files/MutableTime_rb.html)

(See the “Full Code” link on that page to download it.)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 14, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/re-modifying-the-hour-in-a-datetime-object-already-created/68716/2 "2006-09-14T19:07:04Z")

</div>

On 2006-09-14 18:46:38 +0200, “Gavin K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> [File: MutableTime.rb](http://phrogz.net/RubyLibs/rdoc/files/MutableTime_rb.html)
> 
> (See the “Full Code” link on that page to download it.)

thanks a lot Gavin… that’s a nice trick for me (I’ll need to  
manipulate dates …)  
I read ‘immutable’ in the class description, but did not link it to my  
problem… tired… need to break !
