简体   繁体   中英

How can I get an hour, minute etc. out of numpy.datetime64 object?

May be I'm to blind or narrow minded to find a smart solution, but I'd like to extract the time of an numpy.datetime64 object and can't find an obvious solution.

Of course I can do this:

import numpy as np
a = np.datetime64('2015-03-23T22:58')
print(a.tolist().time())

But that still leaves me with handling timezone problems.

Additionally it just doesn't make sense to me to use numpy.datetime64 just to convert it to datetime and use these methods. Or is numpy.datetime64 lacking some major features/I'm not use it as intended and should better fall back to datetime.datetime?

In addition to my comment, I experimented with a simple example to see what the formats had to offer from the documentation reference

>>> import numpy as np
>>> a = np.datetime64('2015-03-23T17:00')  # 17hr local time
>>> b = np.datetime64('2015-03-23T17:00Z') # 17hr Zulu
>>> a
numpy.datetime64('2015-03-23T17:00-0400')
>>> b
numpy.datetime64('2015-03-23T13:00-0400')
>>> 
>>> a.tolist().time()
datetime.time(21, 0)
>>> b.tolist().time()
datetime.time(17, 0)
>>> 

which is the appropriate time difference for my locale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM