moduledatetime
1. class date(builtins.object)
date(year, month, day) --> date object
参数:年月日为三个整数,最大值9999,12,31,最小值1,1,1;返回值为日期对象。
>>> import datetime as d >>> [i for i in dir(d) if i[0]>='a'] ['date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo'] >>> help(d.date) Help on class date in module datetime: class date(builtins.object) | date(year, month, day) --> date object | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __eq__(self, value, /) | Return self==value. | | __format__(...) | Formats self with strftime. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __le__(self, value, /) | Return self<=value. | | __lt__(self, value, /) | Return self<value. | | __ne__(self, value, /) | Return self!=value. | | __radd__(self, value, /) | Return value+self. | | __reduce__(...) | __reduce__() -> (cls, state) | | __repr__(self, /) | Return repr(self). | | __rsub__(self, value, /) | Return value-self. | | __str__(self, /) | Return str(self). | | __sub__(self, value, /) | Return self-value. | | ctime(...) | Return ctime() style string. | | isocalendar(...) | Return a 3-tuple containing ISO year, week number, and weekday. | | isoformat(...) | Return string in ISO 8601 format, YYYY-MM-DD. | | isoweekday(...) | Return the day of the week represented by the date. | Monday == 1 ... Sunday == 7 | | replace(...) | Return date with new specified fields. | | strftime(...) | format -> strftime() style string. | | timetuple(...) | Return time tuple, compatible with time.localtime(). | | toordinal(...) | Return proleptic Gregorian ordinal. January 1 of year 1 is day 1. | | weekday(...) | Return the day of the week represented by the date. | Monday == 0 ... Sunday == 6 | | ---------------------------------------------------------------------- | Class methods defined here: | | fromisocalendar(...) from builtins.type | int, int, int -> Construct a date from the ISO year, week number and weekday. | | This is the inverse of the date.isocalendar() function | | fromisoformat(...) from builtins.type | str -> Construct a date from the output of date.isoformat() | | fromordinal(...) from builtins.type | int -> date corresponding to a proleptic Gregorian ordinal. | | fromtimestamp(timestamp, /) from builtins.type | Create a date from a POSIX timestamp. | | The timestamp is a number, e.g. created via time.time(), that is interpreted | as local time. | | today(...) from builtins.type | Current date or datetime: same as self.__class__.fromtimestamp(time.time()). | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | day | | month | | year | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | max = datetime.date(9999, 12, 31) | | min = datetime.date(1, 1, 1) | | resolution = datetime.timedelta(days=1) >>>
例:
>>> import datetime >>> datetime.date(2021,5,16) datetime.date(2021, 5, 16) >>> Yesterday=datetime.date(2021,5,16) >>> Today=datetime.date.today() >>> Today.__eq__(Yesterday) False >>> Yesterday.__le__(Today) True >>> Yesterday.ctime() 'Sun May 16 00:00:00 2021' >>> Yesterday.isocalendar() (2021, 19, 7) >>> Today.isocalendar() (2021, 20, 1) >>> datetime.date(2019,1,1).isocalendar() (2019, 1, 2) >>> datetime.date(2021,1,1).isocalendar() (2020, 53, 5) >>> datetime.date(2021,1,3).isocalendar() (2020, 53, 7) >>> datetime.date(2021,1,4).isocalendar() (2021, 1, 1) >>> #方法.isocalendar返回值是一年中的第几周第几天 >>> Yesterday.isoformat() '2021-05-16' >>> Today.replace(2020) datetime.date(2020, 5, 17) >>> Today.replace(2021,1) datetime.date(2021, 1, 17) >>> Today.replace(2021,5,16) datetime.date(2021, 5, 16) >>> Yesterday.isoweekday() 7 >>> Today.isoweekday() 1 >>> Yesterday.weekday() 6 >>> Today.weekday() 0 >>> Today.timetuple() time.struct_time(tm_year=2021, tm_mon=5, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=137, tm_isdst=-1) >>> Today.toordinal() 737927 >>> Yesterday.toordinal() 737926 >>> #方法.toordinal()返回值以‘1-1-1’为第1天 >>>
2. class date(builtins.object)
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object
参数:时[分秒...],最少一个参数;返回值为时间对象。
>>> help(datetime.time) Help on class time in module datetime: class time(builtins.object) | time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object | | All arguments are optional. tzinfo may be None, or an instance of | a tzinfo subclass. The remaining arguments may be ints. | | Methods defined here: | | __eq__(self, value, /) | Return self==value. | | __format__(...) | Formats self with strftime. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __le__(self, value, /) | Return self<=value. | | __lt__(self, value, /) | Return self<value. | | __ne__(self, value, /) | Return self!=value. | | __reduce__(...) | __reduce__() -> (cls, state) | | __reduce_ex__(...) | __reduce_ex__(proto) -> (cls, state) | | __repr__(self, /) | Return repr(self). | | __str__(self, /) | Return str(self). | | dst(...) | Return self.tzinfo.dst(self). | | isoformat(...) | Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM]. | | The optional argument timespec specifies the number of additional terms | of the time to include. Valid options are 'auto', 'hours', 'minutes', | 'seconds', 'milliseconds' and 'microseconds'. | | replace(...) | Return time with new specified fields. | | strftime(...) | format -> strftime() style string. | | tzname(...) | Return self.tzinfo.tzname(self). | | utcoffset(...) | Return self.tzinfo.utcoffset(self). | | ---------------------------------------------------------------------- | Class methods defined here: | | fromisoformat(...) from builtins.type | string -> time from time.isoformat() output | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | fold | | hour | | microsecond | | minute | | second | | tzinfo | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | max = datetime.time(23, 59, 59, 999999) | | min = datetime.time(0, 0) | | resolution = datetime.timedelta(microseconds=1)