python 从日期范围中获取季度日期
例子 :
start date = 01/04/2020
end date = 01/04/2021
Run Code Online (Sandbox Code Playgroud)
在这里,我需要获取该日期范围内的第四纪日期。
我正在运行 Python 3.8.3,我发现 ISO Week 格式 (%V) 有些奇怪:
2019 年的第一天和最后一天都在第一周。
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Run Code Online (Sandbox Code Playgroud)
输出:
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Run Code Online (Sandbox Code Playgroud)
为什么它会这样?
我正在尝试将以下字符串转换为 python 中的日期时间。参考datetime.strptime(\xe2\x80\x982017-01-12T14:12:06.000-0500\xe2\x80\x99,'%Y-%m-%dT%H:%M:%S.%f %Z')我正在尝试下面提到的格式。
\nconfig_current_ts = datetime.strptime(internal_config["timestamp_str"], "%Y:%m:%dT%H:%M:%S.%f%z")\nRun Code Online (Sandbox Code Playgroud)\n但我收到一条错误消息:
\n\n\nValueError: 时间数据 \'2021-01-18T11:18:10.833876+00:00\' 与格式\n不匹配 \'%Y:%m:%dT%H:%M:%S.%f%z\ '
\n
我使用的是python3.7.4。有人可以告诉我如何将其转换为日期时间吗?我想基本上比较字符串和当前时间,看看哪个在前面。
\n我最近遇到了一些检查日期时间对象的代码,并认为可以稍微清理一下:
# original
if (datetime1 and (datetime2 is None)):
do_things()
# slightly cleaner version
if (datetime1 and not datetime2):
do_things()
Run Code Online (Sandbox Code Playgroud)
但我意识到,如果存在一个计算结果为 False 的有效日期时间对象,这两个语句并不完全相同。例如:
if not '':
print('beep')
else:
print('boop')
# output: beep
if '' is None:
print('beep')
else:
print('boop')
# output: boop
Run Code Online (Sandbox Code Playgroud)
在Python中查找有关日期时间/时间/日期对象的布尔值的信息时,我一无所获。有谁知道Python是如何处理这个问题的?如果我遇到不同对象类型的类似问题,是否有可靠的方法来检查其布尔值何时为 true 或 false?
我使用datetime,我有:
delta = ((datetime.date.today() + datetime.timedelta(days=1)) - datetime.timedelta(???)).isoformat()
Run Code Online (Sandbox Code Playgroud)
我需要更换??? 从一天到月底.
怎么做?
我有一个UTC时间戳,我想在Python中计算从那时到现在的小时数.我怎样才能做到这一点?
谢谢!
是否有可能实现这样的目标?
def subtract_from_date(date, time_unit, num):
return date - timedelta(time_unit=num)
Run Code Online (Sandbox Code Playgroud)
基本上我觉得懒得放,如果elif条件适用于各种输入时间单位,如分钟,小时,周,天等.
我读了许多类似的问题,函数是用于API的循环的简单嵌套,每分钟我可以调用5次.所以我为一年的数据设定了75的范围.你能帮助我解决这个问题吗?提前致谢!
for zip in zipcode:
url4 = url1+str(zip)+url2
Run Code Online (Sandbox Code Playgroud)
for x in range (0,75):
counter = x * 5
startdate += datetime.timedelta(days=counter)
enddate += datetime.timedelta(days=counter)
url = url4+startdate+","+enddate+url3
apifunction(url)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "<pyshell#51>", line 3, in <module>
startdate += datetime.timedelta(days=counter)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
Run Code Online (Sandbox Code Playgroud) 日期时间值,例如day = "2017-9-2 00:00:00",
在模板中使用时,
index.html
...
{{day}}
...
Run Code Online (Sandbox Code Playgroud)
Django自动将其转换为Sept. 2, 2017, midnight。
如何禁用此行为?
我希望它以其原始格式显示"2017-9-2 00:00:00"。
我正在使用Python 3.7并尝试datetime从库中学习模块,但是我正在获取AttributeError。下面的代码:
import datetime
t = datetime.time(4, 20, 1)
# Let's show the different components
print(t)
print('hour :', t.hour)
print('minute:', t.minute)
print('second:', t.second)
print('microsecond:', t.microsecond)
print('tzinfo:', t.tzinfo)
Run Code Online (Sandbox Code Playgroud)
作为运行文件,我得到此错误 AttributeError: module 'datetime' has no attribute 'time'
请帮助我找到我要去的地方。谢谢
python runtime-error attributeerror python-3.x python-datetime
python-datetime ×10
python ×9
datetime ×3
python-3.x ×3
date ×1
django ×1
pandas ×1
python-2.7 ×1
timedelta ×1
truthiness ×1
utc ×1