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$
1
  • $\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, 2015 at 16:41

2 Answers 2

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$
1
  • $\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, 2019 at 10:59

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.