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

Mic*_*son 3 python datetime 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

sus*_*n97 8

一些合适的括号就足够了:

(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
Run Code Online (Sandbox Code Playgroud)

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


小智 6

我需要使用执行前 1 天的标头来标记文件,因此这对我有用:

\n\n
from datetime import date, timedelta, datetime\nheader = (datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")\n
Run Code Online (Sandbox Code Playgroud)\n\n

这样“标题”就成了我的字符串

\n\n

对于类 datetime.timedelta ,有效参数是:

\n\n
days=0\nseconds=0\nmicroseconds=0\nmilliseconds=0\nminutes=0\nhours=0\nweeks=0\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n

对于 strftime()

\n\n
%a  Weekday as locale\xe2\x80\x99s abbreviated name.   Mon\n%A  Weekday as locale\xe2\x80\x99s full name.  Monday\n%w  Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.   1\n%d  Day of the month as a zero-padded decimal number.   30\n%-d Day of the month as a decimal number. (Platform specific)   30\n%b  Month as locale\xe2\x80\x99s abbreviated name. Sep\n%B  Month as locale\xe2\x80\x99s full name.    September\n%m  Month as a zero-padded decimal number.  09\n%-m Month as a decimal number. (Platform specific)  9\n%y  Year without century as a zero-padded decimal number.   13\n%Y  Year with century as a decimal number.  2013\n%H  Hour (24-hour clock) as a zero-padded decimal number.   07\n%-H Hour (24-hour clock) as a decimal number. (Platform specific)   7\n%I  Hour (12-hour clock) as a zero-padded decimal number.   07\n%-I Hour (12-hour clock) as a decimal number. (Platform specific)   7\n%p  Locale\xe2\x80\x99s equivalent of either AM or PM. AM\n%M  Minute as a zero-padded decimal number. 06\n%-M Minute as a decimal number. (Platform specific) 6\n%S  Second as a zero-padded decimal number. 05\n%-S Second as a decimal number. (Platform specific) 5\n%f  Microsecond as a decimal number, zero-padded on the left.   000000\n%z  UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).    \n%Z  Time zone name (empty string if the object is naive).   \n%j  Day of the year as a zero-padded decimal number.    273\n%-j Day of the year as a decimal number. (Platform specific)    273\n%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\n%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\n%c  Locale\xe2\x80\x99s appropriate date and time representation.  Mon Sep 30 07:06:05 2013\n%x  Locale\xe2\x80\x99s appropriate date representation.   09/30/13\n%X  Locale\xe2\x80\x99s appropriate time representation.   07:06:05\n%%  A literal \'%\' character.    %\n
Run Code Online (Sandbox Code Playgroud)\n