Categories
Python Answers

How to convert Python datetime to epoch with strftime?

Spread the love

Sometimes, we want to convert Python datetime to epoch with strftime.

In this article, we’ll look at how to convert Python datetime to epoch with strftime.

How to convert Python datetime to epoch with strftime?

To convert Python datetime to epoch with strftime, we can call the timestamp method.

For instance, we write:

import datetime

t = datetime.datetime(2020, 4, 1, 0, 0).timestamp()
print(t)

We call timestamp on the datetime object and assign the returned timestamp to t.

Therefore, t is 1585699200.0.

Conclusion

To convert Python datetime to epoch with strftime, we can call the timestamp method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *