[英]UTC time in python with datetime
How can I use datetime.utcnow()
and datetime.date.today()
together?如何一起使用
datetime.utcnow()
和datetime.date.today()
? In case I am running code A it throws error and Code B other one.如果我正在运行代码 A,它会抛出错误,而代码 B 会抛出另一个错误。 I want to use both of these in my code.
我想在我的代码中使用这两个。
A一个
from datetime import datetime, timedelta
path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
for month in range(start_month_number, 13):
this_month = datetime.date.today().replace(year=year, month=month, day=1)
print(this_month)
error - AttributeError: 'method_descriptor' object has no attribute 'today'
B乙
import datetime
path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
for month in range(start_month_number, 13):
this_month = datetime.date.today().replace(year=year, month=month, day=1)
print(this_month)
error- AttributeError: module 'datetime' has no attribute 'utcnow'
Code B run fine in case there is no line -->curryear = datetime.utcnow().strftime('%Y')如果没有行,代码 B 运行良好 -->curryear = datetime.utcnow().strftime('%Y')
Either import the module that you need, or the classes you need from the module - not both.要么导入你需要的模块,要么从模块中导入你需要的类——不能同时导入。 Then, write your code according to what you have imported:
然后,根据您导入的内容编写代码:
A: A:
from datetime import datetime, date
path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
for month in range(1, 13):
this_month = date(year=year, month=month, day=1)
print(this_month)
or B:或 B:
import datetime
path = datetime.datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
for month in range(1, 13):
this_month = datetime.date(year=year, month=month, day=1)
print(this_month)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.