📜  Python DateTime weekday() 方法与示例

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

Python DateTime weekday() 方法与示例

在本文中,我们将讨论 DateTime 模块中的 weekday()函数。 weekday()函数用于根据给定的 DateTime 获取周数。它将返回 0-6 范围内的数字

RepresentationMeaning
0Monday
1Tuesday
2Wednesday
3Thursday
4Friday
5Saturday
6Sunday

它将以“(YYYY, MM, DD, HH, MM, SS)”的格式将输入作为DateTime,其中,

  • YYYY 代表年份
  • MM 代表月
  • DD 代表日期
  • HH 代表小时
  • MM代表分钟
  • SS 代表秒

我们首先需要导入 DateTime 模块并创建一个 DateTime,现在使用 weekday()函数我们将获得特定 DateTime 的工作日。

句法:

我们还可以使用以下语法从 DateTime 中提取日期:

句法:

示例:创建日期时间并显示日期时间和日期的Python程序

Python3
# importing datetime class
from datetime import datetime
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the date
print("Date is :", x.date())


Python3
# importing datetime class
from datetime import datetime
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
# create datetime
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())


Python3
# create a list of weekdays
from datetime import datetime
  
days = ["Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday"]
  
# importing datetime class
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("weekday is :", days[x.weekday()])
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name  using index
print("weekday is :", days[x.weekday()])
  
# create datetime using index
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("weekday is :", days[x.weekday()])


输出:

示例:获取给定日期时间的工作日的Python程序



蟒蛇3

# importing datetime class
from datetime import datetime
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
# create datetime
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())

输出:

示例3:获取工作日名称的Python程序

蟒蛇3

# create a list of weekdays
from datetime import datetime
  
days = ["Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday"]
  
# importing datetime class
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("weekday is :", days[x.weekday()])
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name  using index
print("weekday is :", days[x.weekday()])
  
# create datetime using index
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("weekday is :", days[x.weekday()])

输出: