How to get UTC time - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to get UTC time (/thread-35878.html) |
How to get UTC time - SriRajesh - Dec-26-2021 Hi, I use below code from datetime import datetime print(datetime.utcnow())my output as below: 2021-12-26 00:25:14.083291 but I want like this: 2021-12-26T00:25:14.083291+0800 can any one suggest how to achieve this. RE: How to get UTC time - snippsat - Dec-26-2021 I would suggest using Pendulum. Then default is UTC and deal much better with time zones than datetime, which need a combination with tzinfo, pytz when dealing with time zones. >>> import pendulum >>> >>> now = pendulum.now() >>> now DateTime(2021, 12, 26, 1, 33, 9, 533323, tzinfo=Timezone('Europe/Berlin')) >>> print(now) 2021-12-26T01:33:09.533323+01:00 RE: How to get UTC time - SriRajesh - Dec-26-2021 (Dec-26-2021, 12:56 AM)snippsat Wrote: I would suggest using Pendulum. NameError: name 'DateTime' is not defined ImportError: cannot import name 'DateTime' from 'datetime' (C:\Users\mekal\Anaconda3\lib\datetime.py) from datetime import DateTime import pendulum now = pendulum.now() now DateTime(2021, 12, 26, 1, 33, 9, 533323, tzinfo=Timezone('Europe/Berlin')) print(now) RE: How to get UTC time - ndc85430 - Dec-26-2021 Why are you trying to import DateTime from the standard library datetime module, when it's clearly a type from pendulum as snippsat's code shows?
RE: How to get UTC time - snippsat - Dec-26-2021 As mention there is no need to import datetime,and in my code i do not do that. Doc Wrote:Pendulum is a Python package to ease datetimes manipulation. |