datetime class
时间: 2023-10-14 22:56:10 浏览: 35
Datetime class is a class in Python's datetime module that provides various methods and attributes to manipulate dates and times. It allows you to work with dates, times, and time intervals, perform arithmetic operations on them, and format them as strings.
You can create a datetime object using the datetime class constructor, specifying the year, month, day, hour, minute, second, and microsecond. Once you have a datetime object, you can access its attributes like year, month, day, hour, etc., and perform operations like addition, subtraction, comparison, and formatting.
Here's an example of creating a datetime object and accessing its attributes:
```python
from datetime import datetime
# Create a datetime object
dt = datetime(2022, 10, 31, 12, 30, 0)
# Access datetime attributes
print(dt.year) # Output: 2022
print(dt.month) # Output: 10
print(dt.day) # Output: 31
print(dt.hour) # Output: 12
print(dt.minute) # Output: 30
print(dt.second) # Output: 0
```
The datetime class also provides methods for formatting the datetime object as a string using the strftime() method and parsing a string into a datetime object using the strptime() method.
Let me know if you have any more questions!