Get Current Timestamp In Python
Saturday, Aug 10, 2024 | 3 minutes read | Update at Saturday, Aug 10, 2024
Python provides different modules and methods in order to get current timestamp. In this tutorial we examine these modules and functions in detail with examples. Here are 10 examples of how to get the current timestamp in Python using different methods:
-
Using
time
module: The builtintime
module provides thetime()
method in order to get current timestamp. Do not confuse the time module with thetime()
method as you can see below.import time timestamp = time.time() print(timestamp)
This returns the current time as a float representing seconds since the epoch.
-
Using
datetime
module: Another time and date related module to get current timestamp is thedatetime
module. The datetime module provides thenow()
method which returns current date and time. Then we can use thetimestamp()
method to get current timestamp.from datetime import datetime timestamp = datetime.now().timestamp() print(timestamp)
This returns the current time as a float representing seconds since the epoch.
-
Using
datetime
module to get the current datetime:from datetime import datetime current_datetime = datetime.now() print(current_datetime)
This returns the current date and time as a
datetime
object. -
Using
datetime
withstrftime
for a formatted timestamp: We can format the timestamp like a string. Thestrftime()
method is used to return current timestamp as formatted string.from datetime import datetime formatted_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(formatted_timestamp)
This returns the current date and time as a formatted string.
-
Using
time
module for a formatted timestamp: We can use the time modulestrftime()
and thetime.localtime()
methods to get current timestamp .import time formatted_timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) print(formatted_timestamp)
This returns the current date and time as a formatted string using the
time
module. -
Using
pandas
to get the current timestamp: Python provides different 3rd party libraries to get current timestamp. Thepandas
module provides theTimestamp.now()
to return current timestamp.import pandas as pd timestamp = pd.Timestamp.now() print(timestamp)
This returns the current timestamp as a
pandas.Timestamp
object. -
Using
datetime
module with timezone information: We can get current timestamp for different timezones. We can use thedatetime.now()
method by providing the timezone information to get specified timezone timestamp.from datetime import datetime, timezone timestamp_with_timezone = datetime.now(timezone.utc) print(timestamp_with_timezone)
This returns the current date and time with UTC timezone information.
-
Using
datetime
to get the current date only: We can get timestamp only for date by callingdatetime
modulenow()
method and then callingdate()
method for only date part not time part.from datetime import datetime current_date = datetime.now().date() print(current_date)
This returns the current date only, without the time.
-
Using
datetime
to get the current time only: We can get timestamp only for date by callingdatetime
modulenow()
method and then callingtime()
method for only date part not time part.from datetime import datetime current_time = datetime.now().time() print(current_time)
This returns the current time only, without the date.
-
Using
arrow
library (third-party) to get the current timestamp: The 3rd partyarrow
module providesnow()
method in order to get current timestamp.import arrow timestamp = arrow.now() print(timestamp)
This returns the current timestamp as an
arrow
object, which provides more flexibility with date and time manipulation.
These examples cover different ways to work with timestamps, dates, and times in Python.