close

Python Convert Datetime to Epoch

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about Python Convert Datetime to Epoch. so without wasting time lets learn about of this.

Python Convert Datetime to Epoch

  1. Convert Datetime to Epoch

    In this method we will learn Python Convert Datetime to Epoch by using timestamp(). So lets learn about of this. import datetime ts= datetime.datetime(2020, 12,1, 0, 0).timestamp() print(ts) Output :1606761000.0

  2. Python Convert Datetime to Epoch

    In this method we will learn how we can Convert Datetime to Epoch by using timegm. So lets learn about of this.import datetime import calendar var1 =datetime.datetime(2020, 12, 1, 0, 0) print (calendar.timegm(var1.timetuple()) ) Output : 1606780800

Method 1: using timestamp()

In this method we will learn how we can Convert Datetime to Epoch by using timestamp(). So lets learn about of this.

import datetime
ts= datetime.datetime(2020, 12,1, 0, 0).timestamp()
print(ts)

Output :

1606761000.0

Method 2: using timegm

In this method we will learn how we can Convert Datetime to Epoch by using timegm. So lets learn about of this.

import datetime
import calendar
var1 =datetime.datetime(2020, 12, 1, 0, 0)
print ( calendar.timegm(var1.timetuple()) )

Output :

1606780800

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment