-2

I am trying to write code.

I could print like that Mongolian time 2019-09-27T15:09:34.915812+08:00 How to print local time zone? Like that "UTC+8"

Student_e
  • 17
  • 8
  • 2
    Possible duplicate of [How do I print a Python datetime in the local timezone?](https://stackoverflow.com/questions/1111317/how-do-i-print-a-python-datetime-in-the-local-timezone) – henrywongkk Sep 27 '19 at 07:26
  • you can use `LocalTimezone` class defined in the `datetime.tzinfo` – Mohsen Sep 27 '19 at 07:39

2 Answers2

1

You could use

pytz library

from datetime import datetime, timedelta
from pytz import timezone
import pytz

utc_8 = timezone("Singapore")
utc_8.zone

fmt = '%Y-%m-%d %H:%M:%S %Z%z'

loc_dt = utc_8.localize(datetime(2019, 9, 27, 15, 9, 34))
print(loc_dt.strftime(fmt)) #2019-09-27 15:09:34 +08+0800
1
>>> import datetime
>>> foo = '2019-09-27T15:09:34.915812+08:00'
>>> bar = datetime.datetime.strptime(foo, '%Y-%m-%dT%H:%M:%S.%f%z')
>>> bar.tzname()
'UTC+08:00'
buran
  • 13,682
  • 10
  • 36
  • 61