0

I am trying to add seconds to a datestamp string that is received from a json object but the datetime function I am trying to use does not allow strings and wants the date to be separated like: datetime.strftime(2011,11,18). Here is what I have:

import requests
from datetime import datetime

def call():
    pay = {'token' : "802ba928cd3ce9acd90595df2853ee2b"}
    r = requests.post('http://challenge.code2040.org/api/dating',
                      params=pay)
    response = r.json()
    time = response['datestamp']
    interval = response['interval']
    utc = datetime.strftime(time, '%Y-%m-%dT&H:%M:%S.%fZ')
    timestamp = (utc-time).total_seconds()
    utc_dt = datetime(time) + timedelta(seconds=timestamp)
    print(utc_dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))

Is there another way I can add time to a ISO8601 datestamp?

martineau
  • 119,623
  • 25
  • 170
  • 301
Demetrius
  • 449
  • 1
  • 9
  • 20
  • 2
    I think you want `datetime.strptime` to convert the string into a datetime object that you can add to – OneCricketeer Nov 01 '16 at 17:41
  • Further, you can get the ISO format of a datetime object by `some_datetime.isoformat()` – sytech Nov 01 '16 at 17:43
  • There is some discussion on this post about parsing ISO8601. http://stackoverflow.com/questions/127803/how-to-parse-an-iso-8601-formatted-date-in-python?rq=1 – OneCricketeer Nov 01 '16 at 17:43
  • What's the purpose of subtracting `utc` and `time` variables? Aren't they both the same value? –  Nov 01 '16 at 17:47

0 Answers0