Search code examples
pythondatetimestrptime

How to convert "2019-11-30T07:00:00+09:00" date into integer?


I want to convert 2019-11-30T07:00:00+09:00 into string.

How should I express the part after '+' sign:

datetime.strptime(self.data[0]["LocalDateTime"],"%Y-%m-%dT%H:%M:%S+")

The date is in ISO8601 format. Thank you.


Solution

  • Try this: ( %z is the UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).:

    datetime.datetime.strptime("2019-11-30T07:00:00+09:00","%Y-%m-%dT%H:%M:%S%z")
    
    # datetime.datetime(2019, 11, 30, 7, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=32400)))
    

    for comparison +04:00 has a different timezone delta:

    datetime.datetime.strptime("2019-11-30T07:00:00+04:00","%Y-%m-%dT%H:%M:%S%z")                                                                                                         
    # datetime.datetime(2019, 11, 30, 7, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))