Python datetime减去1天使用timedelta

时间:2018-01-31 03:44:02

标签: python datetime timedelta

我希望超链接中的第一个Array在第二天之前的1天,这是今天的日期。我读了一下datetime,但我没看到它是如何在超链接中应用的。

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 {{ 1}})+'%22%20和%20gameDate%3C =%22 timedelta)+'%22%20和%20gameTypeId = 2%20和%20gameLocationCode =%22H%22

2 个答案:

答案 0 :(得分:3)

一些放置好的括号应该足够了:

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

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

答案 1 :(得分:1)

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

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

使“标题”成为我的字符串

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

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

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

毫秒数转换为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.    %