piyush daga
piyush daga

Reputation: 501

How to remove the additional time-zone added information from datetime string

Silly Question, but couldn't find the proper answer. I converted my datetime object from UTC to ist using dateutils.

utc = datetime.utcnow()
UTC = tz.gettz('UTC')
indian = tz.gettz('Asia/Kolkata')
ind = utc.replace(tzinfo=UTC)
ind.astimezone(indian).replace(microsecond=0).__str__()

Output

'2019-07-30 16:32:04+05:30'

I would like to remove the +5:30 part, how do I go about doing that, except splitting the string on '+' symbol, or how do I avoid it being added in the first place.

Upvotes: 1

Views: 2234

Answers (2)

Håken Lid
Håken Lid

Reputation: 23064

You can simply strip out the timezone from the datetime object by using tzinfo=None. The string representation will still be ISO8601, but with no timezone offset part.

str(
   datetime.now(tz=tz.gettz('Asia/Kolkata')))
   .replace(microseconds=0, tzinfo=None)
)
# '2019-07-30 16:32:04'

Upvotes: 2

rafaelc
rafaelc

Reputation: 59274

You can explicitly state your format via strftime

>>> new = ind.astimezone(indian).replace(microsecond=0)
>>> new.strftime('%Y %m %d %H:%M:%S')
'2019 07 30 16:59:56'

Upvotes: 2

Related Questions