Home > Article > Backend Development > Summary of usage and common methods of datetime module in Python
This article brings you a summary of the usage and common methods of the datetime module in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The datetime module re-encapsulates the time module to provide more interfaces. The classes provided are:
date, a class representing date
time, a class representing time
datetime, a class representing date and time
timedelta, representing a time interval, that is, the interval between two points in time
tzinfo, information related to time zones
(Objects of these classes are all immutable)
(Classes have class methods, class methods have methods, and methods inherit class methods)
1. Date class
##datetime.date(year, month, day)
Commonly used class methods and attributes:
date.max | The maximum value that an object can represent Date (9999-12-31) | ||||||||||||||||||||||||||||||||||||||||||
date.min | The minimum date that the object can represent (0001 -01-01) | ||||||||||||||||||||||||||||||||||||||||||
Returns a date object representing the current local time | |||||||||||||||||||||||||||||||||||||||||||
The date object represents the smallest unit of date (days) | |||||||||||||||||||||||||||||||||||||||||||
Returns a date object based on the given timestamp |
d.year | 年 | ||||||||||||||||||||||||||||||||||||||||||
d.month | month | ||||||||||||||||||||||||||||||||||||||||||
d.day | 日 | ||||||||||||||||||||||||||||||||||||||||||
##d.replace(year[,month[,day]]) | Generate and return a new date object, the original date object remains unchanged #Return the time tuple (time.struct_time) object corresponding to the date | ||||||||||||||||||||||||||||||||||||||||||
##d.toordinal() | |||||||||||||||||||||||||||||||||||||||||||
d .weekday() |
The return date is the day of the week, [0,6], 0 means Monday, 1 means Tuesday | ||||||||||||||||||||||||||||||||||||||||||
The return date is the day of the week, [1,7], 1 means Monday, 2 means Tuesday | |||||||||||||||||||||||||||||||||||||||||||
Returns a tuple in the format of (year, weekday, isoweekday) | |||||||||||||||||||||||||||||||||||||||||||
Returns the date string in 'YYYY-MM-DD' format | |||||||||||||||||||||||||||||||||||||||||||
Custom format string (same as strftime() method of time module) |
time.max | 表示的最大时间 |
time.min | 表示的最小时间 |
time.resolution | 时间的最小单位,这里是1微秒 |
常用的方法与属性:
t.hour | 时 |
t.minute | 分 |
t.second | 秒 |
t.microsecond | 微秒 |
t.tzinfo | 时区信息 |
t.replace() | 用参数指定替代(时,分,秒,微秒,时区)对象,生成并返回新的对象 |
t.isoformat() | 返回'HH:MM:SS'格式字符串 |
t.strftime() | 返回自定义格式化字符串 |
3. Datetime class
is equivalent to combining date and time
datetime.datetime(year,month,day[,hour[,minute[,second[,microsecond[,tzinfo]]]]])
## Commonly used class methods and Properties:
Maximum date | |||||||||||||||||||||||||||||||||||||||||||
Minimum date | |||||||||||||||||||||||||||||||||||||||||||
The minimum unit of the date represented by the datetime object, 1 microsecond | ##datetime.today() | ||||||||||||||||||||||||||||||||||||||||||
Return the current local time | datetime.now([tz]) | ||||||||||||||||||||||||||||||||||||||||||
Return the current local time Time, if tz is specified, returns the local time in the tz time zone | datetime.utcnow() | ||||||||||||||||||||||||||||||||||||||||||
Returns the current UTC Time | datetime.fromtimestamp(timestamp[,tz]) | ||||||||||||||||||||||||||||||||||||||||||
Returns a datetime object based on the given timestamp , if tz is specified, the tz time zone datetime object | datetime.utcfromtimestamp(timestamp) | ||||||||||||||||||||||||||||||||||||||||||
according to the timestamp Create a datetime object | datetime.combine(date, time) | ||||||||||||||||||||||||||||||||||||||||||
Integrate the specified date and time objects For datetime objects | datetime.strftime(date_string,format) | ||||||||||||||||||||||||||||||||||||||||||
Convert the formatted string to a datetime object |
实现: 类方法 import datetime import time print(datetime.datetime.resolution) print(datetime.datetime.today()) print(datetime.datetime.now()) print(datetime.datetime.utcnow()) print(datetime.datetime.fromtimestamp(time.time())) print(datetime.datetime.utcfromtimestamp(time.time())) print(datetime.datetime.combine(datetime.date(2019, 3, 5), datetime.time(3, 2, 45))) print(datetime.datetime.strftime(datetime.date(2019,9,2),'%Y-%m-%d %X')) result 0:00:00.000001 2018-09-17 20:32:36.868500 2018-09-17 20:32:36.868500 2018-09-17 12:32:36.868500 2018-09-17 20:32:36.868500 2018-09-17 12:32:36.868500 2019-03-05 03:02:45 2019-09-02 00:00:00 其中常用的方法与属性:
|
The above is the detailed content of Summary of usage and common methods of datetime module in Python. For more information, please follow other related articles on the PHP Chinese website!