Get Current Timestamp Using Python
Last Updated :
20 Sep, 2024
A timestamp is a sequence of characters that represents the date and time at which a particular event occurred, often accurate to fractions of a second. Timestamps are essential in logging events, tracking files, and handling date-time data in various applications.
Here, we will learn how to get the current timestamp in Python.
How to Get the Current Timestamp in Python
There are 3 different ways to get the current timestamp in Python, We can use functions from modules time, datetime, and calendar.
1. Using the time Module
The time module provides various time-related functions. The function time returns the time in seconds since the epoch as a floating point number. epoch is defined as the point where the time starts and is platform-dependent.
Syntax: time.time()
Parameters: NA
Return: floating point number expressed in seconds.
python
# using time module
import time
# ts stores the time in seconds
ts = time.time()
# print the current timestamp
print(ts)
Output:
1594819641.9622827
Use Case: This method is useful when you need to work with formatted dates and times but still want the ability to extract the corresponding Unix timestamp.
2. Using the datetime Module
The datetime module provides classes for manipulating dates and times. While date and time arithmetic is supported, the target of the implementation is on efficient attribute extraction for output formatting and manipulation. The function datetime.datetime.now which return number of seconds since the epoch.
Syntax: datetime.now()
Parameters: tz (time zone) which is optional.
Return: the current local date and time.
python
# 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:
current time:- 2020-07-15 14:30:26.159446
timestamp:- 1594823426.159446
Use Case: This method is useful when you need to work with formatted dates and times but still want the ability to extract the corresponding Unix timestamp.
3. Using the calendar Module
We can also get timestamp by combining multiple functions from multiple modules. In this we’ll use function calendar.timegm to convert tuple representing current time.
Syntax: calendar.timegm(tuple)
Parameters: takes a time tuple such as returned by the gmtime() function in the time module.
Return: the corresponding Unix timestamp value.
python
# 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:
gmt:- time.struct_time(tm_year=2020, tm_mon=7, tm_mday=15, tm_hour=19, tm_min=21, tm_sec=6, tm_wday=2, tm_yday=197, tm_isdst=0) timestamp:- 1594840866
Use Case: This method is helpful when dealing with UTC or when you need to convert structured time data (like from gmtime()
) into a timestamp
Summary
time.time()
: Returns the current time in seconds as a floating-point number since the epoch. Best for simple time calculations and logging.datetime.now()
: Provides the current local date and time. Use .timestamp()
to convert to Unix time. Ideal for date-time manipulation and formatting.calendar.timegm()
: Converts a structured time tuple to a Unix timestamp. Useful when working with UTC or structured time data
Similar Reads
Get current date using Python
Prerequisite: DateTime module In Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some function
3 min read
How to Get Current Date and Time using Python
In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python. Different ways to get Current Date and Time using PythonCurrent time using DateTime objectGet time using the time moduleGet Current Date and Time Using the Datetime ModuleI
6 min read
Get current time in milliseconds using Python
In this article, we will get the time in milliseconds using Python. Here, we are using two inbuild modules of Python that is Datetime module and the Time Module to get the time in milliseconds. Get time in milliseconds using the DateTime module To get the time in milliseconds we used the DateTime mo
2 min read
Get Current Time in different Timezone using Python
Timezone is defined as a geographical area or region throughout which standard time is observed. It basically refers to the local time of a region or country. Most of the time zones are offset from Coordinated Universal Time (UTC), the world's standard for time zone. In order to get the current time
4 min read
Get UTC timestamp in Python
UTC timestamp represents a specific point in time as measured from the "Unix epoch" â January 1, 1970, 00:00:00 UTC. This timestamp is often used in computing because it is not affected by time zones or daylight saving time, providing a consistent reference time across the globe. When working with t
3 min read
Python | Pandas Timestamp.tzinfo
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.tzinfo attribute is used to check the timezone of the given Timestamp
2 min read
Python | Pandas Timestamp.hour
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.hour attribute return the value of the hour for the given Timestamp o
2 min read
Python | Pandas Timestamp.ceil
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.ceil() function return a new Timestamp ceiled to this resolution. The
2 min read
Python | Pandas Timestamp.tz_convert
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.tz_convert() function convert tz-aware Timestamp to another time zone
2 min read
Convert Datetime to UTC Timestamp in Python
Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it's often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this ar
3 min read