Python Unix Timestamp Snippets
Python examples for current Unix time, timestamp-to-datetime conversion, UTC handling, milliseconds, and formatted date output using the standard library.
import time
int(time.time())import time
int(time.time() * 1000)import datetime
datetime.datetime.fromtimestamp(1700000000)import datetime
datetime.datetime.utcfromtimestamp(1700000000)
# Python 3.2+:
datetime.datetime.fromtimestamp(1700000000, tz=datetime.timezone.utc)import datetime
dt = datetime.datetime.fromtimestamp(1700000000)
dt.strftime("%Y-%m-%d %H:%M:%S")import datetime
dt = datetime.datetime(2023, 11, 15, 6, 13, 20, tzinfo=datetime.timezone.utc)
int(dt.timestamp())Python timestamp basics
Python's time.time() returns seconds since the Unix epoch as a floating-point number. Wrap it with int() for whole Unix seconds, or multiply by 1000 when you need milliseconds.
Timezone-aware datetime output
Use datetime.fromtimestamp(ts, tz=datetime.timezone.utc) for explicit UTC output. Avoid relying on the server's local timezone when generating values for logs, APIs, or database rows.
Python production notes
Python makes it easy to create both naive and timezone-aware datetime values, so be deliberate about which one crosses a system boundary. Use timezone-aware UTC datetimes for APIs, scheduled jobs, and database writes. Convert to a local timezone only for display, reports, or business rules that explicitly depend on a local calendar.
- Use int(time.time()) for whole Unix seconds in scripts and command-line tools
- Use int(time.time() * 1000) only when the receiving system expects milliseconds
- Use datetime.fromtimestamp(ts, tz=timezone.utc) instead of local fromtimestamp for portable output
- Use .isoformat() when you need a readable UTC value in logs or JSON
Datetime today in Python: datetime.today() vs datetime.now()
Common Python searches "datetime today python", "today timestamp python", or "datetime now python utc" are answered by the datetime module. datetime.today() and datetime.now() are nearly identical — both return a naive local datetime. For modern code prefer datetime.now(timezone.utc) which returns a timezone-aware UTC value that can be safely converted to a Unix timestamp.
- datetime.today() → naive datetime in local timezone (no tzinfo attached)
- datetime.now() → same as today() unless you pass a tz argument
- datetime.now(timezone.utc) → timezone-aware UTC datetime (preferred for new code)
- datetime.now(timezone.utc).timestamp() → current Unix epoch seconds as float
- date.today() → today's date with no time component (a date object, not datetime)
- int(time.time()) → current Unix seconds as int — the shortest form
- time.time_ns() → current epoch nanoseconds (Python 3.7+)
String to datetime in Python, datetime to string
Python users searching "string to datetime python", "py datetime to string", or "datetime to string" want strptime/strftime or datetime.fromisoformat(). Use fromisoformat() for ISO 8601 input (Python 3.7+; 3.11 supports full ISO 8601 including UTC offset).
- String to datetime python: datetime.fromisoformat('2024-03-15T14:30:00+00:00') — preferred for ISO 8601
- String to datetime python (custom format): datetime.strptime('2024-03-15 14:30:00', '%Y-%m-%d %H:%M:%S')
- Py datetime to string / datetime to string: dt.isoformat() returns '2024-03-15T14:30:00+00:00'
- Py datetime to string (custom): dt.strftime('%Y-%m-%d %H:%M:%S') returns '2024-03-15 14:30:00'
- ISO 8601 round-trip: datetime.fromisoformat(s).isoformat() — produces same string back
- For Unix timestamp: dt.timestamp() (seconds float); int(dt.timestamp()) for whole seconds
- Python convert string to date / python convert string to date / convert string to date — datetime.fromisoformat(s) for ISO 8601; datetime.strptime(s, fmt) for custom formats
- Python date-only output: date.fromisoformat('2024-03-15') returns a date object (no time)
FAQ
- Does time.time() return an integer?
- No. It returns seconds as a floating-point value. Wrap it with int() for whole Unix seconds, or multiply before rounding when you need milliseconds.
- Should datetime values be naive or timezone-aware?
- Use timezone-aware UTC datetime values for APIs, databases, and logs. Naive datetime values are best avoided at system boundaries because their timezone meaning is implicit.