In this post, we are going to understand 6 ways to get current Timestamp in Python with code examples. To get the DateTime information Python provides an in-built DateTime module, Using it we can extract hours, minutes, seconds, microseconds, days of the week, months, years, days of the month, different dates, and time format, and timestamp data.
1. Time Module to get current Timestamp
The time module provides many functions to get the time. The time() function return the time in the number of seconds elapsed since the epoch.
Python Program to get current Timestamp
import time
timesecondEpoch = time.time()
print('secondEpoch :',timesecondEpoch)
#return struct_time
time_obj = time.localtime(timesecondEpoch)
print('\nStruct_time:\n',time_obj)
Output
secondEpoch : 1626190503.672068
Struct_time:
time.struct_time(tm_year=2021, tm_mon=7, tm_mday=13, tm_hour=21, tm_min=5, tm_sec=3, tm_wday=1, tm_yday=194, tm_isdst=0)
How to Access the member property of Time object in Python
Access the member property of time_obj to convert struct_time to the current timestamp in string format.
timesecondEpoch = time.time()
time_obj = time.localtime(timesecondEpoch)
#converting struct_time to current timestamp in string format
print('struct_time to current timestamp:\n %d-%d-%d %d:%d:%d' % (
time_obj.tm_mday, time_obj.tm_mon, time_obj.tm_year, time_obj.tm_hour, time_obj.tm_min, time_obj.tm_sec))
Output
struct_time to current timestamp:
13-7-2021 21:5:3
2. Time.ctime() method to get current Timestamp
We can get the current timestamp using the time module ctime() method. It takes a second since epoch and converts it into the string format. If seconds are not given then it returns the current timestamp.
Program to get current Timestamp using Time.ctime()
import time
currenttime_str = time.ctime()
print(currenttime_str)
Output
Wed Jul 14 02:05:06 2021
3. Using Time Module gmtime() method
In this example we are using the gmttime() method of the time module and later converting it to the current timestamp using strftime()
Python Program to get current timestamp
import time
gmt = time.gmtime()
print("GMT Time :", gmt)
print('\n gmt struct_time to current timestamp:\n %d-%d-%d %d:%d:%d' % (
gmt.tm_mday, gmt.tm_mon, gmt.tm_year, gmt.tm_hour, gmt.tm_min, gmt.tm_sec))
# using the strtime() function
print("\nGMT: "+time.strftime("%a, %d %b %Y %I:%M:%S %p %Z",gmt))
Output
GMT Time : time.struct_time(tm_year=2021, tm_mon=7, tm_mday=13, tm_hour=20, tm_min=53, tm_sec=0, tm_wday=1, tm_yday=194, tm_isdst=0)
gmt struct_time to current timestamp:
13-7-2021 20:53:0
GMT: Tue, 13 Jul 2021 08:53:00 PM USA Standard Time
4.Using calender Module
The current timespan can be get using the calendar module along with the time module gmt() function.
Program Example
import calendar
import time
curr_time_stamp = calendar.timegm(time.gmtime())
print(curr_time_stamp)
Output
1626213256
5. Using Datetime module
To get the current date and time we are going to use datetime.now() method of class datetime define in the datetime module.
Syntax
datetime.now(timezone=none)
Return: The datetime object returns date and timestamp information of the given time-zone else returns the local timezone.
Program example : Get current Timestamp
from datetime import datetime
dt_obj = datetime.now()
print('current datetime :',dt_obj)
Output
current datetime : 2021-04-07 18:53:14.763064
Access Member property of datetime object
By using datetime class, which has property day, month, year, hour, minute, second, and millisecond, we can get all information of datetime using these properties.
Program Example
from datetime import datetime
dt_obj = datetime.now()
print('yyyy-mm-dd =',dt_obj.year, '-',dt_obj.month, '-',dt_obj.day)
print('Timestamp hh:mm:ss:ms =' ,dt_obj.hour, ':', dt_obj.minute, ':', dt_obj.second, ':', dt_obj.microsecond)
Output
date in yyyy-mm-dd = 2021 - 4 - 7
Timestamp hh:mm:ss:ms = 19 : 46 : 31 : 567594
In the above code, we have used datetime class object property to access date and timestamp information.
Convert datetime to string format
Instead of accessing each member property of datetime class object. We can use strftime() method to get datetime in a different format by using one line of code as we are doing in the below example.
from datetime import datetime
dt_obj = datetime.now()
#date and time in a different format :# dd/mm/YY H:M:S
dt_cust = dt_obj.strftime("%m/%d/%Y %H:%M:%S")
print(" date and time in mm/dd/YY H:M:S format :", dt_cust)
Output
date and time in mm/dd/YY H:M:S format : 04/07/2021 20:54:47
6. Get Current date only using datetime
In Python, the current date can be got in two ways:
- date():To get the date using date() method, need to create object of datetime class object.
- Today():Using the today() method of date class.
from datetime import date,datetime
#using datetime class date() method
dt_date = datetime.now().date()
#using date class today() method
todaydate = date.today()
print('datetime =',dt_date)
print('Using date class: today date =',todaydate)
Output
Using datetime class: Tdoay date = 2021-07-13
Using date class: today date = 2021-07-13
7. How to get current time only using DateTime Module
To get the time in python we can use the time() method of datetime class. We can get all information of time by directly print the object or else access all properties like hours, min, etc.
Program Example
from datetime import datetime
time_now = datetime.now().time()
print('current time =',time_now)
#get all property of date using the time_now object
print('timestamp property =',time_now.hour, ':' , time_now.minute,':', time_now.second,':' , time_now.microsecond)
This is the output we will get
current time = 01:17:20.087430
timestamp property = 1 : 41 : 43 : 276050
Summary :
We have explored in Python 5 ways to get current Timestamp using different multiple modules Using time module, calendar Module, Datetime module.