How to get Timestamp in Python
A timestamp is a sequence of characters or encoded information used to find when a particular event occurred, generally giving the date and time of the day, accurate to a small fraction of a second.
There are different ways to get current timestamp in Python, We can use functions from modules time
, and datetime
1. time module
The time
module provides various time-related functions. The function time, return floating point number expressed in seconds.
Syntax: time.time()
>>> import time >>> ts = time.time() >>> print(ts) 1609422960.200444
2. datetime module
The datetime
module provides classes for manipulating dates and times.
The function datetime.datetime.now return the number of seconds since the epoch.
Syntax: datetime.now(tz) tz (time zone) which is optional.
>>> import datetime >>> ct = datetime.datetime.now() >>> print("Current time is ", ct) Current time is 2020-12-31 19:28:21.077644 >>> ts = ct.timestamp() >>> print("Timestamp is ", ts) Timestamp is 1609423101.077644 >>>
Subscribe
Login
Please login to comment
0 Discussion