📜  Python DateTime – DateTime 类

📅  最后修改于: 2022-05-13 01:54:31.368000             🧑  作者: Mango

Python DateTime – DateTime 类

顾名思义,DateTime 模块的 DateTime 类包含有关日期和时间的信息。与日期对象一样,DateTime 假定当前的公历是双向扩展的;就像一个时间对象,DateTime 假设每天正好有 3600*24 秒。但与日期类不同的是,DateTime 类的对象是潜在的感知对象,即它也包含有关时区的信息。

句法:

年、月和日参数是强制性的。 tzinfo 可以为 None,其余所有属性必须是以下范围内的整数 –

  • MINYEAR(1) <= 年 <= MAXYEAR(9999)
  • 1 <= 月 <= 12
  • 1 <= 天 <= 给定月份和年份中的天数
  • 0 <= 小时 < 24
  • 0 <= 分钟 < 60
  • 0 <= 秒 < 60
  • 0 <= 微秒 < 1000000
  • 折叠 [0, 1]

注意:传递整数以外的参数将引发 TypeError,传递超出范围的参数将引发 ValueError。



示例:创建 DateTime 类的实例

Python3
# Python program to
# demonstrate datetime object
  
from datetime import datetime
  
# Initializing constructor
a = datetime(2022, 10, 22)
print(a)
  
# Initializing constructor
# with time parameters as well
a = datetime(2022, 10, 22, 6, 2, 32, 5456)
print(a)


Python3
from datetime import datetime
  
# Getting min datetime
mindatetime = datetime.min
print("Min DateTime supported", mindatetime)
  
# Getting max datetime
maxdatetime = datetime.max
print("Max DateTime supported", maxdatetime)


Python3
from datetime import datetime
  
# Getting Today's Datetime
today = datetime.now()
  
# Accessing Attributes
print("Day: ", today.day)
print("Month: ", today.month)
print("Year: ", today.year)
print("Hour: ", today.hour)
print("Minute: ", today.minute)
print("Second: ", today.second)


Python3
from datetime import datetime
  
# Getting Today's Datetime
today = datetime.now()
print("Today's date using now() method:", today)
  
today = datetime.today()
print("Today's date using today() method:", today)


Python3
from datetime import datetime
  
# Getting Datetime from timestamp
date_time = datetime.fromtimestamp(1887639468)
print("Datetime from timestamp:", date_time)
  
# Getting Datetime from ordinal
date_time = datetime.fromordinal(737994)
print("Datetime from ordinal:", date_time)


输出
2022-10-22 00:00:00
2022-10-22 06:02:32.005456

类属性

让我们看看这个类提供的属性——

Attribute NameDescription
minThe minimum representable DateTime
maxThe maximum representable DateTime
resolutionThe minimum possible difference between datetime objects
yearThe range of year must be between MINYEAR and MAXYEAR
monthThe range of month must be between 1 and 12
dayThe range of day must be between 1 and number of days in the given month of the given year
hourThe range of hour must be between 0 and 24 (not including 24)
minuteThe range of minute must be between 0 and 60 (not including 60)
secondThe range of second must be between 0 and 60 (not including 60)
microsecondThe range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfoThe object containing timezone information
foldRepresents if the fold has occurred in the time or not

示例 1:获取可表示的最小和最大 DateTime 对象

蟒蛇3

from datetime import datetime
  
# Getting min datetime
mindatetime = datetime.min
print("Min DateTime supported", mindatetime)
  
# Getting max datetime
maxdatetime = datetime.max
print("Max DateTime supported", maxdatetime)
输出
Min DateTime supported 0001-01-01 00:00:00
Max DateTime supported 9999-12-31 23:59:59.999999

示例 2:访问日期和时间对象的属性

蟒蛇3

from datetime import datetime
  
# Getting Today's Datetime
today = datetime.now()
  
# Accessing Attributes
print("Day: ", today.day)
print("Month: ", today.month)
print("Year: ", today.year)
print("Hour: ", today.hour)
print("Minute: ", today.minute)
print("Second: ", today.second)
输出
Day:  26
Month:  7
Year:  2021
Hour:  16
Minute:  24
Second:  7

类函数

DateTime 类提供了各种函数来处理 DateTime 对象,例如我们可以将 DateTime 对象转换为字符串,将字符串转换为 DateTime 对象,我们还可以获取特定月份中特定日期的工作日,我们还可以设置时间特定 DateTime 对象的区域等。



DateTime 类方法列表

Function NameDescription
astimezone()Returns the DateTime object containing timezone information.
combine()Combines the date and time objects and return a DateTime object
ctime()Returns a string representation of date and time
date()Return the Date class object
fromisoformat()Returns a datetime object from the string representation of the date and time
fromordinal()Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. The hour, minute, second, and microsecond are 0
fromtimestamp()Return date and time from POSIX timestamp
isocalendar()Returns a tuple year, week, and weekday
isoformat()Return the string representation of date and time
isoweekday()Returns the day of the week as integer where Monday is 1 and Sunday is 7
now()Returns current local date and time with tz parameter
replace()Changes the specific attributes of the DateTime object
strftime()Returns a string representation of the DateTime object with the given format
strptime()Returns a DateTime object corresponding to the date string
time()Return the Time class object
timetuple()Returns an object of type time.struct_time
timetz()Return the Time class object
today()Return local DateTime with tzinfo as None
toordinal()Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1
tzname()Returns the name of the timezone
utcfromtimestamp()Return UTC from POSIX timestamp
utcoffset()Returns the UTC offset
utcnow()Return current UTC date and time
weekday()Returns the day of the week as integer where Monday is 0 and Sunday is 6

示例 1:获取今天的日期

蟒蛇3

from datetime import datetime
  
# Getting Today's Datetime
today = datetime.now()
print("Today's date using now() method:", today)
  
today = datetime.today()
print("Today's date using today() method:", today)

输出

示例 2:从时间戳和序数获取日期时间

蟒蛇3

from datetime import datetime
  
# Getting Datetime from timestamp
date_time = datetime.fromtimestamp(1887639468)
print("Datetime from timestamp:", date_time)
  
# Getting Datetime from ordinal
date_time = datetime.fromordinal(737994)
print("Datetime from ordinal:", date_time)
输出
Datetime from timestamp: 2029-10-25 16:17:48
Datetime from ordinal: 2021-07-23 00:00:00

注意:有关Python日期时间的更多信息,请参阅Python日期时间教程