Search code examples
pythondatetimetimezonearrow-python

Parse date and time from string with time zone using Arrow


I have

import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'

How can I parse this with Arrow such that I get an Arrow object with the time zone tz? The following defaults to UTC time.

In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss')
Out[30]: <Arrow [2015-12-01T19:00:00+00:00]>

I know the .to function but that converts a time zone and but doesn't allow me to change to time zone.


Solution

  • Try this:

    arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=tz)
    

    If you are also using dateutil, this works as well:

    arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=dateutil.tz.gettz(tz))
    

    So does this:

    arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=dateutil.tz.gettz(tz))