Python 使用 timedelta 将日期时间减去 1 天

Python datetime minus 1 day using timedelta

我希望超链接中的第一个 datetime 比第二个日期早 1 天,即今天的日期。我阅读了一些关于 timedelta 的内容,但我没有看到它是如何在超链接中应用的。

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

一些放置得当的括号就足够了:

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

一个友好的建议:看看 Python string formatting 而不是使用连接构造字符串。它最终会变得更干净,重复更少。

我需要在执行前 1 天用 header 标记文件,所以这对我有用:

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

这样 "header" 就成了我的字符串

对于 class datetime.timedelta 有效参数是:

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

所有参数都是可选的,默认为 0。参数可以是整数或浮点数,可以是正数或负数。内部仅存储天、秒和微秒。参数转换为这些单位:

1 毫秒转换为 1000 微秒。 一分钟转换为 60 秒。 一个小时转换为 3600 秒。 一周转换为 7 天。

对于 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.    %