Search code examples
pythondatetimepython-dateutil

How to convert date string to ISO8601 standard?


using dateutil I was able to genrate ISO8601 of type YYYY-MM-DDThh:mm:ssTZD but I want the format 'YYYY-MM-DDThh:mm:ss.sssTZD' eg '2015-05-13T18:05:55.320-07:00'

My code is

from datetime import datetime
from dateutil.parser import *
from dateutil.tz import *
import dateutil
import time

ts = time.time()
utc_offset = (datetime.fromtimestamp(ts) -
           datetime.utcfromtimestamp(ts)).total_seconds()

now = parse("11/10/09 11:20 PM")
now = now.replace(tzinfo=tzoffset('PDT', utc_offset))

and output is 2009-11-10T23:20:00-07:00 how would I get output like 2009-11-10T23:20:00.000-07:00


Solution

  • Use datetime.isoformat():

    >>> now = datetime.now(tzoffset('EDT', -4*60*60))
    >>> print(now.isoformat())
    2015-05-15T07:08:34.478784-04:00
    

    Note that the isoformat method omits microseconds if the value is 0 (as in your example parsed time. If you must have factional seconds in your output use %f in a format string:

    >>> print(now.strftime("%y-%m-%dT%H:%M:%S.%f%z"))
    15-05-15T07:35:00.000000-0400
    

    If you need milliseconds, you'll have to write your own formatting routine:

    def format_time_with_milliseconds(time):
        fmt = "{time.year:04}-{time.month:02}-{time.day:02}T{time.hour:02}:{time.minute:02}:{time.second:02}.{millisecond:03}{tz}"
        return fmt.format(time=time, millisecond=int(round(time.microsecond/1000)), tz=time.strftime("%z"))