Python datetime

Python datetime

Article directory


datetime It is a built-in module in Python that provides classes for processing dates and times. The following are datetime some of the most commonly used classes in modules:

  1. datetime.date: A class representing date, including three attributes: year, month, and day.
  2. datetime.time: A class representing time, including four attributes: hours, minutes, seconds, and microseconds.
  3. datetime.datetime: A class representing date and time, including all attributes of dateand .time
  4. datetime.timedelta: A class representing a time interval, used to calculate the difference between two dates or times.

Here are some datetimeexamples of using the module to handle dates and times:

import datetime  
  
# 获取当前日期和时间  
now = datetime.datetime.now()  
print(now)  
  
# 创建指定日期和时间  
dt = datetime.datetime(2023, 3, 13, 14, 30, 0)  
print(dt)  
  
# 计算时间差  
delta = datetime.timedelta(days=3, hours=2, minutes=30)  
print(delta)  
  
# 在当前时间上加上时间差  
new_time = now + delta  
print(new_time)

Output:

2023-03-13 14:30:00.000000  
2023-03-13 14:30:00  
3 days, 2:30:00  
2023-03-16 17:00:00.000000

General catalog of "AUTOSAR lineage decomposition (ETAS tool chain)"

Guess you like

Origin blog.csdn.net/PlutoZuo/article/details/132849608