杰瑞科技汇

python timestamp now

Of course! Getting the current timestamp in Python is a common task. The best and most modern way to do this is with the datetime module.

python timestamp now-图1
(图片来源网络,侵删)

Here's a breakdown of the methods, from the most recommended to other alternatives.

The Best & Most Common Way: datetime.now()

This is the standard, readable, and flexible approach. It returns a datetime object, which you can easily format or convert to other timestamp formats.

Get a datetime Object (Human-Readable)

This is useful when you want to display the date and time in a specific format.

from datetime import datetime
# Get the current date and time as a datetime object
now = datetime.now()
print(f"Current datetime object: {now}")
print(f"Type: {type(now)}")
# Example output:
# Current datetime object: 2025-10-27 10:30:00.123456
# Type: <class 'datetime.datetime'>

Get a Unix Timestamp (Seconds since Epoch)

A Unix timestamp is the number of seconds that have elapsed since January 1, 1970 (UTC). This is the most common format for storing timestamps in databases and for use in APIs.

python timestamp now-图2
(图片来源网络,侵删)

You can easily convert the datetime object to a Unix timestamp.

from datetime import datetime
# Get the current datetime object
now = datetime.now()
# Convert the datetime object to a Unix timestamp (seconds since epoch)
timestamp_seconds = int(now.timestamp())
print(f"Current Unix timestamp (seconds): {timestamp_seconds}")
print(f"Type: {type(timestamp_seconds)}")
# Example output:
# Current Unix timestamp (seconds: 1698388200
# Type: <class 'int'>

Get a High-Resolution Timestamp (Floating-Point Seconds)

If you need more precision than whole seconds, you can use the .timestamp() method directly without converting to an int. This gives you fractional seconds.

from datetime import datetime
# Get the current datetime object
now = datetime.now()
# Get the timestamp with fractional seconds
timestamp_float = now.timestamp()
print(f"Current Unix timestamp (float): {timestamp_float}")
print(f"Type: {type(timestamp_float)}")
# Example output:
# Current Unix timestamp (float): 1698388200.123456
# Type: <class 'float'>

Alternative: time.time()

The time module also provides a way to get the current time. Its time() function directly returns a floating-point number representing the time in seconds since the epoch.

This is slightly faster than datetime.now().timestamp() if you only need the raw timestamp and don't need any of the datetime object's formatting capabilities.

python timestamp now-图3
(图片来源网络,侵删)
import time
# Get the current time in seconds since the epoch (as a float)
timestamp_float = time.time()
print(f"Current Unix timestamp (float) using time.time(): {timestamp_float}")
print(f"Type: {type(timestamp_float)}")
# Example output:
# Current Unix timestamp (float) using time.time(): 1698388200.123456
# Type: <class 'float'>

When to use time.time() vs datetime.now().timestamp()?

  • Use time.time() for high-performance applications where you just need the raw number and don't plan to format it.
  • Use datetime.now().timestamp() when you might also need to work with the date and time in a human-readable format, as it gives you a datetime object that is more versatile.

Summary: Which One Should You Use?

Your Goal Recommended Method Example
Get the current date and time to display datetime.now() from datetime import datetime; print(datetime.now())
Get a timestamp for a database or API (seconds) int(datetime.now().timestamp()) from datetime import datetime; print(int(datetime.now().timestamp()))
Get a high-precision timestamp (float) datetime.now().timestamp() or time.time() from datetime import datetime; print(datetime.now().timestamp())
Just need the fastest possible raw timestamp time.time() import time; print(time.time())

Bonus: Formatting the datetime Object

A huge advantage of the datetime object is its easy formatting using the strftime() method.

from datetime import datetime
now = datetime.now()
# Format the datetime object into a string
# %Y = Year, %m = Month, %d = Day
# %H = Hour, %M = Minute, %S = Second
formatted_string = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted datetime: {formatted_string}")
# Example output:
# Formatted datetime: 2025-10-27 10:30:00
分享:
扫描分享到社交APP
上一篇
下一篇