I am trying to convert a datetime from UTC to another timezone. But after the conversion, the time component is not changing. Instead it's adding the offset time along with the timezone.
Is there any way I can display the actual time in that timezone?
Sample code:
>>> import datetime
>>> import pytz
>>> today=datetime.datetime.now()
>>> today
datetime.datetime(2019, 4, 18, 0, 50, 33, 294610)
>>> today.isoformat()
'2019-04-18T00:50:33.294610'
>>> today2=today.astimezone(pytz.timezone('Asia/Singapore'))
>>> today2
datetime.datetime(2019, 4, 18, 0, 50, 33, 294610, tzinfo=<DstTzInfo 'Asia/Singapore' +08+8:00:00 STD>)
>>> today2.isoformat()
'2019-04-18T00:50:33.294610+08:00'
>>> today2.strftime("%Y-%m-%d %H:%M:%S")
'2019-04-18 00:50:33'
>>>
I expect today2
variable to print: 2019-04-18T08:50:33.294610
I tried formatting the date using strftime("%Y-%m-%d %H:%M:%S")
, but it's still showing me 2019-04-18 00:50:33
.
Please help. Thanks.