我正在设计一个类,该类的date
参数默认为今天。我知道datetime.now()
提供了当前时间戳,但是无法找到将datetime
实例转换为的方法date
。显然,我可以这样做,date(datetime.now().year, datetime.now().month, datetime.now().day)
但是这很丑陋,而且在一天,一个月或一年结束时运行代码的机会极少,这会导致类的实例不一致。
请仅使用Python 3.x解决方案。
一种选择是使用静态方法。
@staticmethod
def _today():
now: datetime = datetime.now()
return date(now.year, now.month, now.day)
def __init__(self, start_date: date = _today(), bucket_size_hour: int = 1):
pass
您可以尝试.date
在datetime对象的末尾添加。调整如何定义datetime
像这样的小钻应该适合您:
datetime = datetime.today().date()
输出:
2019-12-26
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句