0

I have a variable storing a date object CreateDate = keyData["CreateDate"] that is storing a value like "2017-01-11 19:40:26+00:00" (depends on the date) and I am wanting to use that date in addition with today's date to find out how many days it has been between the two. So for example if the create date was 2017-07-10 and today's date is 2017-07-18 then I would like the output to be the amount of days passed. I have tried working with the datetime module like datetime.combine(date.today(), createdate) - datetime.combine(date.today(), todaysDate) but still run into issues and think there might be a simpler way to get the total days passed between the two.

        todaysDate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        todaysDate = str(todaysDate)
        todaysDate = todaysDate[0:10]

        todaysDate = datetime.strptime(todaysDate, "%Y-%m-%d")
        CreateDate = datetime.strptime(CreateDate, "%Y-%m-%d")
        totalDays =  abs((CreateDate - todaysDate).days)
        print (totalDays)

`

jelidens
  • 221
  • 1
  • 4
  • 14
  • Possible duplicate: [difference between dates](https://stackoverflow.com/questions/8419564/difference-between-two-dates) ? – Ryan Jul 18 '17 at 15:37
  • I did look at that in order and tried this from is, `datetime.combine(date.today(), createdate) - datetime.combine(date.today(), todaysDate)` But I at this point have two strings, that look like a variation of "2017-01-11" and "2017-07-14" and I am wanting to find the days between the two – jelidens Jul 18 '17 at 15:39
  • 1
    You can just use the code that is in the top answer from that link and it should work. I tried it out with the dates that you listed above and got the correct answer of 8 days. Do you need help formatting the input so that it works with that function? – Ryan Jul 18 '17 at 15:45
  • Thank you, I did sort of get it to work with that function, but now working with ` datetime.datetime(2017, 1, 11, 0, 0) is not JSON serializable` error – jelidens Jul 18 '17 at 16:00
  • Can you post the code that is generating that error? If you have a string in the form of 'YYYY-MM-DD' then the code should work. Maybe something else is causing the error? – Ryan Jul 18 '17 at 16:05
  • I added on the code in the original post at the end. It runs through at the beginning ok and prints out the correct numbers but then gets to a point I guess where it stops and errors: `An error occurred during JSON serialization of response: datetime.datetime(2017, 1, 11, 0, 0) is not JSON serializable` – jelidens Jul 18 '17 at 16:16
  • I think the issue is actually unrelated. Thank you for the help! I have gotten it working aside from just needing to indicate a stopping point to fix the above error else where in a larger segment of the code. – jelidens Jul 18 '17 at 16:23

0 Answers0