0

from response i get date time stamp like this '1663935188183'. I used python datetime.fromtimestamp() function and print date in full format '2022-09-23 14:13:08.183000'

My question is. Can i extract from function above just hours, minutes and secods ?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • There are multiple possibilities. I would recommend a look into the python docs https://docs.python.org/3/ There you will find everything you need. attributes that give you the values one by one, strftime(), isoformat(). – Jon_Kle Sep 23 '22 at 17:38
  • Also take a look at this: https://stackoverflow.com/help/how-to-ask – Jon_Kle Sep 23 '22 at 17:42
  • 1
    Does this answer your question? [Converting unix timestamp string to readable date](https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date) and [How can I extract hours and minutes from a datetime.datetime object](https://stackoverflow.com/q/25754405/10197418) – FObersteiner Sep 23 '22 at 18:06

1 Answers1

0

You can try this solution

>> from datetime import datetime

# Be carefule this will raise ValueError: year 54698 is out of range
# you have to divide timestamp by 1e3 if your timestamp is in milliseconds
>> timestamp = 1663935188183/1e3
>> date = datetime.fromtimestamp(timestamp)
>> date.hour, date.minute, date.second
(14, 13, 8) 
farch
  • 315
  • 4
  • 14
  • that timestamp is probably milliseconds, not nanoseconds. so correct divisor is 1e3. also, your answer is missing how to extract hours seconds etc. from the datetime object. – FObersteiner Sep 23 '22 at 18:03
  • thank you for your comment, forgot to mention that we can also string format the date to obtain ```hours```, ```minutes```, ```seconds``` and ```nanoseconds``` using the following function ```date.strftime('%H:%M:%S.%f')``` – farch Sep 24 '22 at 09:07