เราดำเนินการปฏิทินโดยใช้ปฏิทิน โมดูลใน Python . เราจะมาเรียนรู้เกี่ยวกับวิธีการต่างๆ ของปฏิทิน ตัวอย่างคลาส
calendar.calendar(ปี)
ปฏิทิน อินสแตนซ์ของคลาสส่งคืนปฏิทินของปี มาดูตัวอย่างกัน
ตัวอย่าง
# importing the calendar module import calendar # initializing year year = 2019 # printing the calendar print(calendar.calendar(year))
ผลลัพธ์
หากคุณเรียกใช้โค้ดข้างต้น คุณจะได้ผลลัพธ์ดังต่อไปนี้

calendar.firstweekday()
วิธีการ calendar.firstweekday() ส่งกลับวันทำงานแรกของสัปดาห์ เช่น MONDAY
ตัวอย่าง
# importing the calendar import calendar # getting firstweekday of the year print(calendar.firstweekday())
ผลลัพธ์
หากคุณเรียกใช้โปรแกรมข้างต้น คุณจะได้ผลลัพธ์ดังต่อไปนี้
0
calendar.isleap(ปี)
วิธีการ calendar.isleap(ปี) ผลตอบแทนไม่ว่าจะเป็นปีกระโดดหรือไม่ ลองมาดูตัวอย่างกัน
ตัวอย่าง
# importing the calendar module
import calendar
# initializing years
year_one = 2019
year_two = 2020
# checking whether they are leap or not
print(f'{year_one} is leap year' if calendar.isleap(year_one) else f'{year_one} is
not a leap year')
print(f'{year_two} is leap year' if calendar.isleap(year_two) else f'{year_two} is
not a leap year') ผลลัพธ์
หากคุณเรียกใช้โค้ดข้างต้น คุณจะได้ผลลัพธ์ดังต่อไปนี้
2019 is not a leap year 2020 is leap year
ปฏิทิน.WEEK_CONSTANT
มี WEEK_CONSTANTS ใน ปฏิทิน โมดูลเพื่อรับหมายเลขวันทำงาน มาดูตัวอย่างกัน
ตัวอย่าง
# importing the calendar module
import calendar
# getting weekday numbers
print(f'Monday weekday: {calendar.MONDAY}')
print(f'Tuesday weekday: {calendar.TUESDAY}')
print(f'Wednesday weekday: {calendar.WEDNESDAY}')
print(f'Thursday weekday: {calendar.THURSDAY}')
print(f'Friday weekday: {calendar.FRIDAY}')
print(f'Saturday weekday: {calendar.SATURDAY}')
print(f'Sunday weekday: {calendar.SUNDAY}') ผลลัพธ์
หากคุณเรียกใช้โปรแกรมข้างต้น คุณจะได้ผลลัพธ์ดังต่อไปนี้
Monday weekday: 0 Tuesday weekday: 1 Wednesday weekday: 2 Thursday weekday: 3 Friday weekday: 4 Saturday weekday: 5 Sunday weekday: 6
บทสรุป
เราได้เรียนรู้วิธีการที่เป็นประโยชน์ในบทช่วยสอนนี้แล้ว หากคุณมีคำถามใดๆ เกี่ยวกับบทแนะนำ โปรดระบุในส่วนความคิดเห็น