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
timemodule: The builtintimemodule 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
datetimemodule: Another time and date related module to get current timestamp is thedatetimemodule. 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
datetimemodule 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
datetimeobject. -
Using
datetimewithstrftimefor 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
timemodule 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
timemodule. -
Using
pandasto get the current timestamp: Python provides different 3rd party libraries to get current timestamp. Thepandasmodule 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.Timestampobject. -
Using
datetimemodule 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
datetimeto get the current date only: We can get timestamp only for date by callingdatetimemodulenow()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
datetimeto get the current time only: We can get timestamp only for date by callingdatetimemodulenow()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
arrowlibrary (third-party) to get the current timestamp: The 3rd partyarrowmodule providesnow()method in order to get current timestamp.import arrow timestamp = arrow.now() print(timestamp)This returns the current timestamp as an
arrowobject, which provides more flexibility with date and time manipulation.
These examples cover different ways to work with timestamps, dates, and times in Python.