1

I get the invalid format string error when I try to run the below code (last line), not sure where I am missing the point:

  import datetime
  DAYS = 2
  SINCE = datetime.datetime.now() - datetime.timedelta(days=DAYS)
  params = "?fields=feed.since(" + SINCE.strftime("%s") + ").limit(1),name,updated_time&"

Any suggestions would be much appreciated !!

5
  • Works fine for me, both in 2 and in 3. Commented Aug 17, 2017 at 15:34
  • Are you passing 'params' into a SQL query? Commented Aug 17, 2017 at 15:35
  • My python version is 3.6.1 and Anaconda version is 4.4.0 (64 bit) Commented Aug 17, 2017 at 15:35
  • @Thomas it doesn't work for me in Python 2. It requires "s" to be capitalised in order to get units of seconds. Otherwise it throws ValueError: Invalid format string Commented Aug 17, 2017 at 15:37
  • @roganjosh Interesting. I guess Python (on Linux) just passes it on to strftime from libc, which does have %s. But maybe on other platforms it's emulated without %s support. Commented Aug 17, 2017 at 15:41

3 Answers 3

3

You have to use "%S" because "%s" is not defined in the method you called : https://docs.python.org/2/library/datetime.html#datetime.strftime

import datetime
DAYS = 2
SINCE = datetime.datetime.now() - datetime.timedelta(days=DAYS)
params = "?fields=feed.since(" + SINCE.strftime("%S") + ").limit(1),name,updated_time&"

You should add what format you need for your application.

Sign up to request clarification or add additional context in comments.

Comments

0

It really depends on which format suits you, but if you need timestamp use:

int(time.mktime(SINCE.timetuple()))

Comments

0

it works fine for me (Python 2.7). If it is part of a query and it is failing on that part, maybe you can use another date format like:

params = "?fields=feed.since(" + SINCE.strftime("%Y-%m-%d %H:%M:%S") + ").limit(1),name,updated_time&"

Please note that capital "S", will give you the seconds of that datetime object.

2 Comments

We are discussing your first point in comments under the main question. It doesn't work in Python 2.7 on Windows, but it seems it does work on Linux.
Well then SINCE.strftime("%Y-%m-%d %H:%M:%S") should work fine in the query. It is a useful workaround.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.