📜  Python DateTime - 日期类

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

Python DateTime - 日期类

Date 类的对象根据当前的公历表示包含年、月和日的原始日期。该日期可以在两个方向无限期延长。第 1 年的 1 月 1 日称为第 1 天,1 月 2 日或第 2 年称为第 2 天,依此类推。

句法:

class datetime.date(year, month, day)

参数必须在以下范围内 -

  • MINYEAR(1) <= 年 <= MAXYEAR(9999)
  • 1 <= 月 <= 12
  • 1 <= 天 <= 给定月份和年份中的天数

注意:如果参数不是整数,则会引发 TypeError,如果超出范围,则会引发 ValueError。

例子:



Python3
# Python program to
# demonstrate date class
 
# import the date class
from datetime import date
 
# initializing constructor
# and passing arguments in the
# format year, month, date
my_date = date(2020, 12, 11)
 
print("Date passed as argument is", my_date)
 
# Uncommenting my_date = date(1996, 12, 39)
# will raise an ValueError as it is
# outside range
 
# uncommenting my_date = date('1996', 12, 11)
# will raise a TypeError as a string is
# passed instead of integer


Python3
from datetime import date
 
# Getting min date
mindate = date.min
print("Min Date supported", mindate)
 
# Getting max date
maxdate = date.max
print("Max Date supported", maxdate)


Python3
from datetime import date
 
# creating the date object
Date = date(2020, 12, 11)
 
# Accessing the attributes
print("Year:", Date.year)
print("Month:", Date.month)
print("Day:", Date.day)


Python3
# Python program to
# print current date
 
from datetime import date
 
# calling the today
# function of date class
today = date.today()
 
print("Today's date is", today)
 
# Converting the date to the string
Str = date.isoformat(today)
print("String Representation", Str)
print(type(Str))


Python3
# Python program to
# print current date
 
from datetime import date
 
# calling the today
# function of date class
today = date.today()
 
# Getting Weekday using weekday()
# method
print("Weekday using weekday():", today.weekday())
 
# Getting Weekday using isoweekday()
# method
print("Weekday using isoweekday():", today.isoweekday())
 
# Getting the proleptic Gregorian
# ordinal
print("proleptic Gregorian ordinal:", today.toordinal())
 
# Getting the date from the ordinal
print("Date from ordinal", date.fromordinal(737000))


输出
Date passed as argument is 2020-12-11

类属性

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

Attribute NameDescription
minThe minimum representable date
maxThe maximum representable date
resolutionThe minimum possible difference between date 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

示例 1:获取最小和最大可表示日期

蟒蛇3

from datetime import date
 
# Getting min date
mindate = date.min
print("Min Date supported", mindate)
 
# Getting max date
maxdate = date.max
print("Max Date supported", maxdate)
输出
Min Date supported 0001-01-01
Max Date supported 9999-12-31

示例 2:从日期类访问年、月和日期属性

蟒蛇3

from datetime import date
 
# creating the date object
Date = date(2020, 12, 11)
 
# Accessing the attributes
print("Year:", Date.year)
print("Month:", Date.month)
print("Day:", Date.day)
输出
Year: 2020
Month: 12
Day: 11

类函数

Date 类提供了各种函数来处理 date 对象,比如我们可以获取今天的日期、当前时间戳的日期、预测格列高利序数的日期,其中第 1 年的 1 月 1 日的序数为 1,等等。让我们看看所有的列表这个类提供的功能——

所有日期类函数的列表

Function Name Description
ctime()Return a string representing the date
fromisocalendar()Returns a date corresponsing to the ISO calendar
fromisoformat()Returns a date object from the string representation of the date
fromordinal()Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1
fromtimestamp()Returns a date object from the POSIX timestamp
isocalendar()Returns a tuple year, week, and weekday
isoformat()Returns the string representation of the date
isoweekday()Returns the day of the week as integer where Monday is 1 and Sunday is 7
replace()Changes the value of the date object with the given parameter
strftime()Returns a string representation of the date with the given format
timetuple()Returns an object of type time.struct_time
today()Returns the current local date
toordinal()Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1
weekday()Returns the day of the week as integer where Monday is 0 and Sunday is 6

让我们看一下上述函数的某些示例



示例 1:获取当前日期并将日期更改为字符串

蟒蛇3

# Python program to
# print current date
 
from datetime import date
 
# calling the today
# function of date class
today = date.today()
 
print("Today's date is", today)
 
# Converting the date to the string
Str = date.isoformat(today)
print("String Representation", Str)
print(type(Str))
输出
Today's date is 2021-07-23
String Representation 2021-07-23

示例 2:从一天和可预测的格里高利序数中获取工作日

蟒蛇3

# Python program to
# print current date
 
from datetime import date
 
# calling the today
# function of date class
today = date.today()
 
# Getting Weekday using weekday()
# method
print("Weekday using weekday():", today.weekday())
 
# Getting Weekday using isoweekday()
# method
print("Weekday using isoweekday():", today.isoweekday())
 
# Getting the proleptic Gregorian
# ordinal
print("proleptic Gregorian ordinal:", today.toordinal())
 
# Getting the date from the ordinal
print("Date from ordinal", date.fromordinal(737000))
输出
Weekday using weekday(): 4
Weekday using isoweekday(): 5
proleptic Gregorian ordinal: 737994
Date from ordinal 2018-11-02

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