# Modifying the hour in a datetime object already created

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

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [September 14, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/modifying-the-hour-in-a-datetime-object-already-created/68712/1 "2006-09-14T18:37:00Z")

</div>

In my prog, I create a datetime object

d1s = DateTime.civil(2006,6,5,16,00,00)

then later on, I need to create another datetime from this one by  
adding 1 day and changing the hour to 10:00:00

I can do easily

d1e = d1s + 1

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

thanks for your help

joss

---

<div class="post-metadata">

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

</div>

On Fri, 15 Sep 2006, Josselin wrote:

> but how can I modify the hour, which stays at 16:00:00 ?
> 
> thanks for your help
> 
> joss

harp:~ \> cat a.rb  
ru  
require ‘date’  
require ‘yaml’

now = DateTime.now

year, yday = now.year, now.yday  
ten\_hours = Rational 10, 24

tomorrow = DateTime.ordinal year, yday + 1  
ten\_oclock\_tomorrow = tomorrow + ten\_hours

y ‘now’ =\> now  
y ‘ten\_oclock\_tomorrow’ =\> ten\_oclock\_tomorrow

## harp:~ \> ruby a.rb

## now: 2006-09-14T11:01:49-0701

ten\_oclock\_tomorrow: 2006-09-15T10:00:00Z

-a
