I'm trying to get the timestamp from an ObjectID, but Mongo keeps giving me this error. Am I missing an import? What's the best way to convert the timestamp to a standard date format?
video['date'] = video['_id'].getTimeStamp()
I'm trying to get the timestamp from an ObjectID, but Mongo keeps giving me this error. Am I missing an import? What's the best way to convert the timestamp to a standard date format?
video['date'] = video['_id'].getTimeStamp()
Where'd you see getTimeStamp()
as a method?
According to the docs it should just be:
video['date'] = video['_id'].generation_time
this answer is python and Django based.
Before using generation_time
be aware that generation_time
will be converted to UTC(Universal Time) which might be ahead or behind depending on your current timezone, so you might see a difference in time if you are using your Local Time in your project settings to save timestamps on your objects. For me, there is a difference of 5.5 hours. So adjust the time according to your timezone.
Example:
For ObjectId('5c51aca67c76124020edbbaf')
the actuall creation time of this object was datetime.datetime(2019, 1, 30, 19, 24, 28, 73000)
but when i geneted its time using generation_time
it is 5.5 hours behind.
In [10]: from bson import ObjectId
In [11]: ObjectId('5c51aca67c76124020edbbaf').generation_time
Out[11]: datetime.datetime(2019, 1, 30, 13, 54, 46, tzinfo=<bson.tz_util.FixedOffset object at 0x102461128>)
Since we get the time in UTC from the object_id of our document, to convert to your local time what you can do is ...
document['_id'].generation_time #(gives the generation/creation time of the object id in UTC.)
utc=document['_id'].generation_time
hours_added = datetime.timedelta(hours = 4)` #(Suppose utc+4 is my local time.)
local_time=utc+hours_added