0

I need to assign to a variable the current datetime string in isoformat like the following:

  2018-09-27T16:19:16+02:00

What I'm doing is:

  import datetime
  ....

  print(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc, microsecond=0).isoformat())

But this is going to print the string with utc tz:

2018-09-28T07:05:35+00:00

Not clear yet to me what's the clean way I should change tzinfo param to set wanted tz to UTC+02:00 ?

Thanks

1 Answer 1

2

utcnow() already gives you the the time at +00:00, if you'd like to recieve the time at a specific timezone, you should provide the timezone as an argument to now([tz]).

https://docs.python.org/3/library/datetime.html

>>> import datetime as dt
>>> dt.datetime.now(tz = dt.timezone(offset = dt.timedelta(hours = 2))).replace(microsecond = 0).isoformat()
'2018-09-28T09:20:19+02:00'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was missing the part : tz=dt.timezone(offset=dt.timedelta(hours=2))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.