code: def formatIBDatetime(dt) -> str:
      
      if not dt:
          s = ''
      elif isinstance(dt, datetime.datetime):
          if dt.tzinfo:
              # convert to local system timezone
              dt = dt.astimezone()
          s = dt.strftime('%Y%m%d %H:%M:%S')
      elif isinstance(dt, datetime.date):
          s = dt.strftime('%Y%m%d 23:59:59')
      else:
          s = dt
      return s
docs: Format date or datetime to string that IB uses.