Search code examples
pythondatetimetimezoneutc

Why does timezone unaware datetime object print out in local timezone?


When I instantiate a datetime object like this:

>>> print(datetime.datetime.now())
2023-09-16 10:30:15.50

I get the correct local time, which is currently around 10:30 AM.

Why is it like this? Shouldn't it default to UTC time or something?


Solution

    • datetime.datetime.now() function returns the current local date and time by default. The local time is determined by the system's time zone settings.
    • If you want to get the current time in UTC, you can use:
    import datetime
    print(datetime.datetime.now(datetime.timezone.utc))