Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could someone help me, thank you very much!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading