how to convert snowflake discord id to date in python?

I'm looking for a way to convert discord user id to date, but I haven't figured out how to do it yet👇

I have this little code created in python

import datetime def snowflake_time(id): return datetime.datetime.utcfromtimestamp(((id >> 22) + 210801244770402304) / 1000) 

But it's not working

Could someone help me, thank you very much!

1 Answer

You're adding the incorrect value to the timestamp part.

According to the Snowflake ID Wikipedia article, all dates start at the Twitter Epoch, which is 1288834974657 milliseconds from the Unix Epoch. Thus, to convert a Snowflake ID timestamp into milliseconds from the Unix Epoch, simply add 1288834974657.

In your code, you add 210801244770402304. I'm not sure where you're getting this number, but it is incorrect. This code worked for me:

import datetime def snowflake_time(snowflake): return datetime.datetime.utcfromtimestamp(((snowflake >> 22) + 1288834974657) / 1000) print(snowflake_time(1541815603606036480)) # => 2022-06-28 16:07:40.105000 
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like