Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python datetime minus 1 day using timedelta

I would like the first datetime in the hyperlink to be 1 day before the second which is today's date. I read a little bit about the timedelta but I did not see how it applied within a hyperlink.

http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameDate%3C=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22

like image 487
Michael T Johnson Avatar asked Jun 08 '26 01:06

Michael T Johnson


1 Answers

Some well placed parentheses should suffice:

(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

A friendly suggestion: take a look at Python string formatting instead of constructing your string using concatenation. It'll end up being a lot cleaner and less repetitive.

like image 190
sushain97 Avatar answered Jun 09 '26 15:06

sushain97



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!