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 use the timestamp method

For instance, we write

ts = datetime.datetime(2022, 4, 1, 0, 0).timestamp()

to create a datetime with datetime.datetime.

And then we call timestamp to return the Unix timestamp in seconds.

Conclusion

To convert Python datetime to epoch with strftime, we use 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 *