简体   繁体   English

为什么datetime.date不提供strptime?

[英]Why does datetime.date not provide strptime?

The python module datetime.datetime provides strftime and strptime . python模块datetime.datetime提供strftimestrptime

datetime.date provides only strftime . datetime.date仅提供strftime

Why is that? 这是为什么?

datetime.strptime is a class method that generates datetime objects. datetime.strptime是一个生成 datetime对象的类方法 Since a datetime is more general than a date object and the latter can be retrieved from the former via .date() and there is no need to reimplement this method (besides having a method called date.strptime would be quite confusing since it mixes terms "date" and "time" while it's actually just a date). 由于datetime datedate对象更通用,并且可以通过.date()date对象中检索date对象,因此无需重新实现此方法(除了使用名为date.strptime的方法之外,因为它混合了术语“日期”和“时间”,而实际上只是一个日期)。

On the other hand datetime.strftime and date.strftime are instance methods that transform a given object. 另一方面, datetime.strftimedate.strftime是转换给定对象的实例方法 In order to provide the same functionality for date objects this methods needs to reimplemented (also because mapping date -> datetime is ambiguous). 为了为date对象提供相同的功能,需要重新实现此方法(同样是因为映射date- date -> datetime不明确)。

Note that in Python 3.7 there was the date.fromisoformat classmethod added, as a convenience method for parsing specific date strings (inverse to date.isoformat ). 请注意,在Python 3.7中,添加了date.fromisoformat 方法,作为解析特定日期字符串(与date.isoformat )的一种便捷方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:strptime()参数1必须是str,而不是datetime.date Python - TypeError: strptime() argument 1 must be str, not datetime.date Python 为什么在使用strptime时出现此datetime日期错误 - Why does this datetime date error comes when using strptime 循环浏览日期字符串列表:strptime输出“ Datetime.date(2011-7-5)”而不是(2011-7-5) - Loop through a list of date strings: strptime outputs “Datetime.date(2011-7-5)” instead of (2011-7-5) datetime.date + datetime.timedelta = datetime.date是什么 - What does datetime.date + datetime.timedelta = datetime.date 这个错误的解决方法是什么? TypeError: strptime() argument 1 must be str, not datetime.date - What is a solution to this error TypeError: strptime() argument 1 must be str, not datetime.date 为什么Python datetime.date与它允许的输入格式不一致? 这是datetime.date中的错误吗? - why is Python datetime.date inconsistent with the input format it allows? is this a bug in datetime.date? 在Python中,为什么isinstance函数说datetime是datetime.date的实例? - In Python, why isinstance function says that a datetime is an instance of datetime.date? datetime.date:TypeError:必须为整数。 为什么? - datetime.date: TypeError: an integer is required. Why? 为什么`datetime.strptime`在2015年第0周的星期二得到错误的日期? - Why does `datetime.strptime` get an incorrect date for Tuesday in the week 0 of 2015? 为什么我不能子类化 datetime.date? - Why can't I subclass datetime.date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM