8

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

Michael T Johnson
  • 679
  • 2
  • 13
  • 26

2 Answers2

15

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.

sushain97
  • 2,752
  • 1
  • 25
  • 36
  • Within the hyper link above I would change the first one to `' + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')`? – Michael T Johnson Jan 31 '18 at 03:55
  • You need to replace `+ datetime.datetime.now().strftime('%Y-%m-%d') + ` with `+ (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') +`. The extra parens are required to ensure that Python doesn't think we're trying to `datetime.timedelta(days=1).strftime('%Y-%m-%d')`. – sushain97 Jan 31 '18 at 03:58
  • so `days=1` means yesterday? replaced the first set of date time to that and it did not return any data.. – Michael T Johnson Jan 31 '18 at 04:09
  • 1
    `days=1` means 1 day. if you do `+ datetime.timedelta(days=1)` that adds one day (tomorrow). If you subtract, you get yesterday. – sushain97 Jan 31 '18 at 04:12
6

I needed to label files with a header that was 1 day prior to execution so this worked for me:

from datetime import date, timedelta, datetime
header = (datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")

so that "header" became my string

For class datetime.timedelta the valid arguments are:

days=0
seconds=0
microseconds=0
milliseconds=0
minutes=0
hours=0
weeks=0

All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative. Only days, seconds and microseconds are stored internally. Arguments are converted to those units:

A millisecond is converted to 1000 microseconds. A minute is converted to 60 seconds. An hour is converted to 3600 seconds. A week is converted to 7 days.

For strftime()

%a  Weekday as locale’s abbreviated name.   Mon
%A  Weekday as locale’s full name.  Monday
%w  Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.   1
%d  Day of the month as a zero-padded decimal number.   30
%-d Day of the month as a decimal number. (Platform specific)   30
%b  Month as locale’s abbreviated name. Sep
%B  Month as locale’s full name.    September
%m  Month as a zero-padded decimal number.  09
%-m Month as a decimal number. (Platform specific)  9
%y  Year without century as a zero-padded decimal number.   13
%Y  Year with century as a decimal number.  2013
%H  Hour (24-hour clock) as a zero-padded decimal number.   07
%-H Hour (24-hour clock) as a decimal number. (Platform specific)   7
%I  Hour (12-hour clock) as a zero-padded decimal number.   07
%-I Hour (12-hour clock) as a decimal number. (Platform specific)   7
%p  Locale’s equivalent of either AM or PM. AM
%M  Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S  Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f  Microsecond as a decimal number, zero-padded on the left.   000000
%z  UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).    
%Z  Time zone name (empty string if the object is naive).   
%j  Day of the year as a zero-padded decimal number.    273
%-j Day of the year as a decimal number. (Platform specific)    273
%U  Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.    39
%W  Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.    39
%c  Locale’s appropriate date and time representation.  Mon Sep 30 07:06:05 2013
%x  Locale’s appropriate date representation.   09/30/13
%X  Locale’s appropriate time representation.   07:06:05
%%  A literal '%' character.    %
FastGTR
  • 343
  • 4
  • 4