4
$\begingroup$

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.

[Edit] 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.

$\endgroup$
10
  • $\begingroup$ Related: (105554) $\endgroup$
    – Mr.Wizard
    Commented Jul 17, 2016 at 17:05
  • $\begingroup$ @Mr.Wizard I had seen that question already, but thanks - edited this question $\endgroup$
    – PHPirate
    Commented Jul 17, 2016 at 17:07
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Jul 17, 2016 at 17:08
  • $\begingroup$ @Mr.Wizard I understand, that is why I had edited the question to include the reference, hope that was right. $\endgroup$
    – PHPirate
    Commented Jul 17, 2016 at 17:09
  • 1
    $\begingroup$ @Mr.Wizard Of course not, you're only trying to help! $\endgroup$
    – PHPirate
    Commented Jul 17, 2016 at 17:13

2 Answers 2

7
$\begingroup$

A slight modification of your strategy ought to work:

DateObject[{2016, 7, 17, 0, 0}] /. _TimeObject -> TimeObject[{18, 0, 0}]
$\endgroup$
6
  • 1
    $\begingroup$ (It works on my gedanken Mathematica, but I would love for somebody to confirm with their installation.) $\endgroup$ Commented Jul 17, 2016 at 17:09
  • $\begingroup$ Yes, it works, and you have an impressive mind to pull that off as consistently as you do. I am openly envious. :^) $\endgroup$
    – Mr.Wizard
    Commented Jul 17, 2016 at 17:10
  • $\begingroup$ Oh, I had to modify the TimeObject... sound logical on hindsight, thanks, it worked straigt away! $\endgroup$
    – PHPirate
    Commented Jul 17, 2016 at 17:10
  • 2
    $\begingroup$ @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. $\endgroup$ Commented Jul 17, 2016 at 17:16
  • 2
    $\begingroup$ @Mr. Wizard, on the flip side, the installation takes a whole lot of time, sometimes interspersed with crashes and hangs. ;) $\endgroup$ Commented Jul 17, 2016 at 17:33
4
$\begingroup$

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}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.