title: how to store and access datetime(s) in a Python object that needs to be converted into JSON?
body: <p>I'm working on my first python script, which creates and updates and object with different datetime entries.</p>
  
  <p>I'm setting up the object like this:</p>
  
  <pre><code># Date conversion
  import datetime
  import time
  
  # 0:01:00 and 0:00:00 threshold and totalseconds
  threshold = time.strptime('00:01:00,000'.split(',')[0],'%H:%M:%S')
  tick = datetime.timedelta(hours=threshold.tm_hour,minutes=threshold.tm_min,seconds=threshold.tm_sec).total_seconds()
  zero_time = datetime.timedelta(hours=0,minutes=0,seconds=0)
  zero_tick = zero_time.total_seconds()
  format_date = '%d/%b/%Y:%H:%M:%S'
  
  from datetime import datetime
  
  # Response object
  class ResponseObject(object):
      def __init__(self, dict):
        self.__dict__ = dict
  
  # JSON encoding
  from json import JSONEncoder
  class MyEncoder(JSONEncoder):
      def default(self, o):
        return o.__dict__
  
  # &gt; check for JSON response object
  try:
     obj
  except NameError:
      obj = ResponseObject({})
  
  ...
  entry = "14/Nov/2012:09:32:31 +0100"
  entry_tz = str.join(' ', entry.split(None)[1:6])
  entry_notz = entry.replace(' '+entry_tz,'')
  this_time = datetime.strptime(entry_notz, format_date)
  
  # &gt; add machine to object if not there, add init time
  if not hasattr(obj, "SOFTINST"):
      #line-breaks for readability
      setattr(obj, "SOFTINST", {  
          "init":this_time,
          "last":this_time,
          "downtime":zero_time,
          "totaltime":"",
          "percentile":100
      })
  ... 
  print this_time
  print MyEncoder().encode({"hello":"bar"})
  print getattr(obj, "SOFTINST")
  </code></pre>
  
  <p>My last 'print' returns this:</p>
  
  <pre><code>{
    'totaltime': datetime.timedelta(0),
    'uptime': '',
    'last': datetime.datetime(2012, 11, 14, 9, 32, 31),
    'init': datetime.datetime(2012, 11, 14, 9, 32, 31),
    'percentile': 100, 
    'downtime': 0
  }
  </code></pre>
  
  <p>Which I cannot convert into JSON...</p>
  
  <p>I don't understand why this:</p>
  
  <pre><code>print this_time   #2012-11-14 09:32:31
  </code></pre>
  
  <p>but inside the object, it's stored as </p>
  
  <pre><code>datetime.datetime(2012, 11, 14, 9, 32, 31)
  </code></pre>
  
  <p><strong>Question:</strong><br>
  How do I store datetime objects in "string format" and still have them easily accessible (and modifyable) in Python?</p>
  
  <p>Thanks!</p>