![]() |
VOOZH | about |
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. There are 3 different ways to get the current timestamp in Python, let's explore these methods.
The time module provides the time() function, which returns the current time in seconds since the Unix epoch.
1746164675.3231642
Explanation: time.time() returns the current time as a floating-point number, where the integer part is the seconds and the decimal part represents fractions of a second.
The datetime module provides the now() function to get the current date and time, and the timestamp() method converts it to a timestamp.
current time: 2025-05-02 05:44:35.538966 timestamp: 1746164675.538966
Explanation:
The calendar module can be used along with time.gmtime() to get the current GMT (Greenwich Mean Time), which can then be converted to a timestamp using calendar.timegm().
gmt: time.struct_time(tm_year=2025, tm_mon=5, tm_mday=2, tm_hour=5, tm_min=44, tm_sec=35, tm_wday=4, tm_yday=122, tm_isdst=0) timestamp: 1746164675
Explanation: