AppDividend
Latest Code Tutorials

How to Get Current Timestamp in Python

0

In Python, you can get current Date and Time using datetime.now() function. Python provides the module datetime, which has the class datetime. Python also provides module time, which is very helpful to get the current Timestamp.

How to Get Current Timestamp in Python

There are four different ways to get a timestamp.

  1. Timestamp using time.time().
  2. Using time.ctime()
  3. Get Timestamp using the datetime module.
  4. Get the current Timestamp using the calendar module.

Python provides a module time, and it has a function time() that returns several seconds that have elapsed since the epoch. For example, January 1, 1970.

First, we have to import the time module using “import time” and then we will be able to use time() function of the time module if you don’t know how to import module then check out how to import module tutorial.

Get current timestamp using time.time() method

See the following code.

import time

secondsSinceEpoch = time.time()
print(secondsSinceEpoch)

Output

1589434816.805749

Your output will be different based on the time you are executing the above code.

We can convert seconds since epoch to struct_time. 

See the code.

import time

secondsSinceEpoch = time.time()
timeObj = time.localtime(secondsSinceEpoch)
print(timeObj)

Output

time.struct_time(tm_year=2020, tm_mon=5, tm_mday=14, 
tm_hour=11, tm_min=14, tm_sec=37, 
tm_wday=3, tm_yday=135, tm_isdst=0)

From the object, you can see that we can access the member variables of the struct_time object to create a current timestamp in string format.

import time

secondsSinceEpoch = time.time()
timeObj = time.localtime(secondsSinceEpoch)

print('Current TimeStamp is : %d/%d/%d %d:%d:%d' % (
timeObj.tm_mday, timeObj.tm_mon, timeObj.tm_year, timeObj.tm_hour, timeObj.tm_min, timeObj.tm_sec))

Output

Current TimeStamp is : 14/5/2020 11:17:31

Get current Timestamp using time.ctime() method

Python time method ctime() converts the time expressed in seconds since the epoch to the string representing local time. If the secs is not provided or None, the current time as returned by time() is used. 

See the following code.

import time

timeString = time.ctime()
print('The current timestamp is: ', timeString)

Output

The current timestamp is:  Thu May 14 11:19:45 2020

Get Timestamp using module datetime

Module datetime provides classes for changing date and time in a more object-oriented way. One of them is the datetime.datetime.now, which returns the number of seconds since the epoch.

import datetime;
ts = datetime.datetime.now().timestamp()
print(ts)

Output

1589435542.715096

Get current Timestamp using module calendar

Python also provides a calendar module. In this case, we will use the calendar.timegm function to convert tuple representing the current time.

See the following code.

import calendar
import time

ts = calendar.timegm(time.gmtime())
print(ts)

Output

1589435637

So, there are lots of ways, and you can generate Timestamp in Python and use it in various ways.

Finally, how to get the current timestamp tutorial is over.

See also

Python Date format

Python Datetime()

Convert Datetime to String

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.