How to elegantly convert from DateObject to TimeObject?

The purpose is to extract time from the DateObject and manipulate only time components afterwards.

  • $\begingroup$ It seems that for the methods below to work, DateObject must contain the time. It is possible to have DateObject without the time, in which case error will be returned. A way around this is to call DateString[dateObject, {"Hour","Minute","Second", "Millisecond"}] $\endgroup$
    – Karolis
    Aug 28 '15 at 16:41

TimeObject can take a DateObject so:

datetime = DateObject[]
time = TimeObject[datetime]

Since the FullForm of a DateObject looks like this:


we can use Cases:

now = DateObject[]
Cases[now, TimeObject[__]][[1]]

enter image description here

Or even simpler:

  • $\begingroup$ Not anymore: probably since v11.1, the full form is DateObject[{2019, 5, 21, 12, 55, 24.6968067}, "Instant", "Gregorian", 2.]. $\endgroup$ May 21 '19 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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