How to convert Python datetime to epoch with strftime?

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