程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python: get the day of the week for the specified date

編輯:Python

brief introduction : week , Also called Zhou , It's a time unit , It is also a working day 、 Basis for rest days . Gets the day of the week when the specified time is , Mainly through datetime、calendar、pendulum, among datetime、calendar All are built-in modules , You can import ;pendulum For third party modules , You need to install .

Related strategies :
Python:zhdate Module lunar date processing
Python:radar Generate random dates 、 Time

install :

pip install pendulum

Source code of case :

# -*- coding: utf-8 -*-
# time: 2022/6/9 14:07
# file: weekday_demo.py
# author: tom
# official account : Play with test development 
import calendar
import datetime
import pendulum
class Weekday(object):
@staticmethod
def method_one(year: int, moth: int, day: int):
""" # weekday_list = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] weekday: return 0-6 Integer between :param year: :param moth: :param day: :return: """
return datetime.datetime(year, moth, day).weekday()
@staticmethod
def method_two(year: int, moth: int, day: int):
""" isoweekday: return 1-7 Integer between :param year: :param moth: :param day: :return: """
weekday_list = ["", " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday ", " Sunday "]
return weekday_list[datetime.datetime(year, moth, day).isoweekday()]
@staticmethod
def method_three(year: int, moth: int, day: int):
""" strftime: English week name of the output date , Parameters : %A :param year: :param moth: :param day: :return: """
return datetime.datetime(year, moth, day).strftime("%A")
@staticmethod
def method_four(year: int, moth: int, day: int):
""" strftime: English week name abbreviation of output date , Parameters : %a :param year: :param moth: :param day: :return: """
return datetime.datetime(year, moth, day).strftime("%a")
@staticmethod
def method_five(year: int, moth: int, day: int):
""" weekday: return 0-6 Integer between :param year: :param moth: :param day: :return: """
return calendar.weekday(year, moth, day)
@staticmethod
def method_six(year: int, moth: int, day: int):
""" day_of_week: return 1-7 Integer between :param year: :param moth: :param day: :return: """
year, moth, day = str(year), str(moth), str(day)
if len(moth) == 1:
moth = "0" + moth
if len(day) == 1:
day = "0" + day
return pendulum.parse(f'{
year}-{
moth}-{
day}').day_of_week
if __name__ == '__main__':
# With 2022-06-09 Wednesday For example ,
w = Weekday()
r1 = w.method_one(2022, 6, 9)
print(f"r1:{
r1}") # r1:3, Thursday 
r2 = w.method_two(2022, 6, 9)
print(f"r2:{
r2}") # r2: Thursday 
r3 = w.method_three(2022, 6, 9)
print(f"r3:{
r3}") # r3:Thursday
r4 = w.method_four(2022, 6, 9)
print(f"r4:{
r4}") # r4:Thu
r5 = w.method_five(2022, 6, 9)
print(f"r5:{
r5}") # r5:3, Thursday 
r6 = w.method_six(2022, 6, 9)
print(f"r6:{
r6}") # r6:4, Thursday 

WeChat official account : Play with test development
Welcome to your attention , Common progress , thank you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved