简体   繁体   中英

Compare and convert two datetime objects in JSON to Python

I am using Python's requests module to fetch data from a website. In the JSON response object, I have a key created at and its value is 1520369492984 .I am totally lost as to how to convert this to a Datetime object in Python. pd.to_datetime failed. Any pointers on this would be much appreciated! Thank you!

What you're getting is called epoch Date Time.

import time
time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(1520369492984/1000))

Or using the datetime module:

import datetime
datetime.datetime.utcfromtimestamp(1520369492984/1000).replace(tzinfo=datetime.timezone.utc)

Background Story:

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'.

You can find online converters for the same. My personal recommendation: epoch converter

The above not only converts the timestamp but also, gives you code snippets for different languages.

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