# Set hour of DateObject

Say I have a date object date = DateObject[{2016, 7, 17, 0, 0}], how do I later change the hour of that date object, without needing to know the value it currently has?

I'm searching for something like date /. Hour -> 18, but can't find it in the documentation, just that

DateObject allows addition and subtraction of time quantities.

 How to modify wrong time zone in a Date object? is related, but I already tried date /. (Hour -> _) :> (Hour -> 18) and that didn't work.

• Related: (105554) – Mr.Wizard Jul 17 '16 at 17:05
• @Mr.Wizard I had seen that question already, but thanks - edited this question – PHPirate Jul 17 '16 at 17:07
• FYI, my "Related" links are usually less about answering the question which they are under and more about site organization with the goal of helping people who later find your but need something just a bit different. – Mr.Wizard Jul 17 '16 at 17:08
• @Mr.Wizard I understand, that is why I had edited the question to include the reference, hope that was right. – PHPirate Jul 17 '16 at 17:09
• @Mr.Wizard Of course not, you're only trying to help! – PHPirate Jul 17 '16 at 17:13

A slight modification of your strategy ought to work:

DateObject[{2016, 7, 17, 0, 0}] /. _TimeObject -> TimeObject[{18, 0, 0}]

• (It works on my gedanken Mathematica, but I would love for somebody to confirm with their installation.) – J. M. is in limbo Jul 17 '16 at 17:09
• Yes, it works, and you have an impressive mind to pull that off as consistently as you do. I am openly envious. :^) – Mr.Wizard Jul 17 '16 at 17:10
• Oh, I had to modify the TimeObject... sound logical on hindsight, thanks, it worked straigt away! – PHPirate Jul 17 '16 at 17:10
• @PHPirate: "DateObject[{y, m, d, h, m, s}] is canonicalized to DateObject[{y, m, d}, TimeObject[{h, m, s}]], where values of m, d, h, m, s outside their normal ranges are appropriately reduced." This is exploited in the solution given above. – J. M. is in limbo Jul 17 '16 at 17:16
• @Mr. Wizard, on the flip side, the installation takes a whole lot of time, sometimes interspersed with crashes and hangs. ;) – J. M. is in limbo Jul 17 '16 at 17:33

This should change the Hour to 18:

do = DateObject[{2016, 7, 17, 0, 0}]

DateObject[do, {18, 0, 0}]


This slightly abridged syntax works as well:

DateObject[do, {18}]