Get UTC timestamp in Python

Get UTC timestamp in Python

To get the current UTC timestamp in Python, you can use the built-in datetime module. Here's how you can do it:

1. Get UTC datetime:

import datetime # Get current UTC datetime utc_datetime = datetime.datetime.utcnow() print(utc_datetime) 

2. Get UTC timestamp:

If by "UTC timestamp", you mean the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC), you can use the following approach:

import datetime # Get current UTC timestamp utc_timestamp = datetime.datetime.utcnow().timestamp() print(utc_timestamp) 

Note that utcnow().timestamp() provides a float value, with the decimal part representing fractions of a second.

Remember, datetime.utcnow() gives the current datetime in UTC without any timezone information attached to it (it's a naive datetime object). If you work with timezones and want a timezone-aware datetime, consider using the pytz library.


More Tags

asp.net-core-1.0 android-hardware rustup aspectj kramdown standard-deviation blazor urlopen security column-sum

More Programming Guides

Other Guides

More Programming Examples