


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')) Copy after login 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 Copy after login 其中常用的方法与属性:
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! Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
![]() Hot AI Tools![]() Undresser.AI UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() AI Hentai GeneratorGenerate AI Hentai for free. ![]() Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
4 weeks ago
By DDD
Two Point Museum: All Exhibits And Where To Find Them
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics![]() The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation. ![]() There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks. ![]() It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone. ![]() An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents. ![]() XML can be converted to images by using an XSLT converter or image library. XSLT Converter: Use an XSLT processor and stylesheet to convert XML to images. Image Library: Use libraries such as PIL or ImageMagick to create images from XML data, such as drawing shapes and text. ![]() To generate images through XML, you need to use graph libraries (such as Pillow and JFreeChart) as bridges to generate images based on metadata (size, color) in XML. The key to controlling the size of the image is to adjust the values of the <width> and <height> tags in XML. However, in practical applications, the complexity of XML structure, the fineness of graph drawing, the speed of image generation and memory consumption, and the selection of image formats all have an impact on the generated image size. Therefore, it is necessary to have a deep understanding of XML structure, proficient in the graphics library, and consider factors such as optimization algorithms and image format selection. ![]() Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse. ![]() XML formatting tools can type code according to rules to improve readability and understanding. When selecting a tool, pay attention to customization capabilities, handling of special circumstances, performance and ease of use. Commonly used tool types include online tools, IDE plug-ins, and command-line tools. ![]() |