Timestamp is a order of characters or encrypted information point out when a certain event occurred, generally it gives date and time of day, sometimes exact to a small fraction of a second.
Get current timestamp using Python
So here we will show you various methods to find out latest date ,time or timestamp in Python.We will import functions from modules time, calendar and date.
1. Using module time :
The function time, return the time in seconds since the date & time as a floating point number.
#using time module import time # ts stores the time in seconds ts = time.time() # print the current timestamp print(ts)
Output:
RESTART: C:/Users/HP/Desktop/article2.py 1618989907.6277814
2. Using module datetime :
The datetime module arranges classes for manipulating dates and times. The function datetime.datetime.now which return number of seconds.
# using datetime module import datetime; # ct stores current time ct = datetime.datetime.now() print("current time:-", ct) # ts store timestamp of current time ts = ct.timestamp() print("timestamp:-", ts)
Output:
RESTART: C:/Users/HP/Desktop/article2.py current time:- 2021-04-21 13:21:06.256878 timestamp:- 1618991466.25687
3. Using module calendar :
We are able to understand timestamp by combining multiple functions by multiple modules.
In this topic we are going to use function calendar.timegm to change tuple showing current time.
# using calendar module # using time module import calendar; import time; # gmt stores current gmtime gmt = time.gmtime() print("gmt:-", gmt) # ts stores timestamp ts = calendar.timegm(gmt) print("timestamp:-", ts)
Output:
RESTART: C:/Users/HP/Desktop/article2.py gmt:- time.struct_time(tm_year=2021, tm_mon=4, tm_mday=21, tm_hour=8, tm_min=18, tm_sec=37, tm_wday=2, tm_yday=111, tm_isdst=0) timestamp:- 1618993117
For Current Date only:
Let assume we do not want complete current timestamp, we just want current date only. Then what to do ?
datetime class consists of 2 other classes date & time . We can get date object from a datetime object.
from datetime import datetime dateTimeObj = datetime.now() dateObj = dateTimeObj.date() print(dateObj)
Output:
RESTART: C:/Users/HP/Desktop/article2.py 2021-04-21
For current Time only:
So here is the process if you want to know only current time.
As datetime module provides a datetime.time class too. We can get time object from a datetime object i.e.
from datetime import datetime import time dateTimeObj = datetime.now() # get the time object from datetime object timeObj = dateTimeObj.time() print(timeObj)
Output:
RESTART: C:/Users/HP/Desktop/article2.py 18:14:51.276231
For Current Timestamp using time.time():
Python gives us a module time & it also has a function time() that returns the number of seconds.
from datetime import datetime import time dateTimeObj = datetime.now() # get the time object from datetime object timeObj = dateTimeObj.time() secondsSinceEpoch = time.time() timeObj = time.localtime(secondsSinceEpoch) # get the current timestamp elements from struct_time object i.e. 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:
RESTART: C:/Users/HP/Desktop/article2.py Current TimeStamp is : 21-4-2021 18:37:31
Get Current Timestamp using time.ctime():
Time module has some other function time.ctime() .
from datetime import datetime import time def ctime(seconds=None): timeStr = time.ctime() print('Current Timestamp : ', timeStr) print(ctime())
Output:
RESTART: C:/Users/HP/Desktop/article2.py Current Timestamp : Wed Apr 21 18:46:44 2021
Conclusion:
So In this article we have seen multiple ways to find out current Date & Time or Timestamp.We have also seen how to use ct.time() and time.time().