5
$\begingroup$

How to elegantly convert from DateObject to TimeObject?

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

$\endgroup$
  • $\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
12
$\begingroup$

TimeObject can take a DateObject so:

datetime = DateObject[]
time = TimeObject[datetime]
$\endgroup$
6
$\begingroup$

Since the FullForm of a DateObject looks like this:

DateObject[List[2015,8,27],TimeObject[List[17,58,23.456762],Rule[TimeZone,-5.]],Rule[TimeZone,-5.]]

we can use Cases:

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

enter image description here

Or even simpler:

now[[2]]
$\endgroup$
  • $\begingroup$ Not anymore: probably since v11.1, the full form is DateObject[{2019, 5, 21, 12, 55, 24.6968067}, "Instant", "Gregorian", 2.]. $\endgroup$ – István Zachar 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.