# nhập mô-đun ngày giờ của Python
nhập ngày giờ
# các ngày trong tuần dưới dạng một tuple
weekDays = ["Thứ Hai","Thứ Ba","Thứ Tư","Thứ Năm","Thứ Sáu","Thứ Bảy","Chủ Nhật"]
# Tìm hiểu xem Giáng sinh năm nay là ngày nào trong tuần
thisXMas = ngày giờ. ngày[25/12/2017]
thisXMasDay = thisXMas. ngày trong tuần[]
thisXMasDayAsString = weekDays[thisXMasDay]
print["Giáng sinh năm nay vào ngày {}". định dạng[thisXMasDayAsString]]
# Tìm hiểu xem năm mới tới là ngày nào trong tuần
nextNewYear = ngày giờ. ngày[2018,1,1]
nextNewYearDay = nextNewYear. ngày trong tuần[]
nextNewYearDayAsString = weekDays[nextNewYearDay]
print["Năm mới tới là ngày {}". định dạng [nextNewYearDayAsString]]
Cho trước một ngày, nhiệm vụ là viết một chương trình Python để lấy ngày làm việc trước đó gần nhất kể từ ngày đã cho
Thí dụ
Đầu vào. test_date = datetime[2020, 1, 31]
đầu ra. 2020-01-30 00. 00. 00
Giải trình. Ngày 31 tháng 1 năm 2020, là thứ Sáu, ngày làm việc cuối cùng là thứ năm, tôi. ngày 30 tháng 1
Đầu vào. test_date = datetime[2020, 2, 3]
đầu ra. 2020-01-31 00. 00. 00
Giải trình. Ngày 3 tháng 2 năm 2020, là Thứ Hai, ngày làm việc cuối cùng là Thứ Sáu, tôi. ngày 31 tháng 1
Phương pháp 1. Sử dụng timedelta[] + ngày trong tuần[]
Trong trường hợp này, chúng tôi thực hiện nhiệm vụ trừ 3 trong trường hợp Thứ Hai, 2 trong trường hợp Chủ nhật và 1 cho tất cả các ngày khác. timedelta[] thực hiện nhiệm vụ trừ và các câu lệnh có điều kiện kiểm tra một ngày trong tuần
Python3
# Python3 code to demonstrate working of
# Last business day
# using timedelta[] + conditional statements + weekday[]
from
datetime
import
datetime, timedelta
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:001
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:004_______0_______5
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:001
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:002
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:003
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:004
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:005
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:007
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:008
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
# Python3 code to demonstrate working of
0
# Python3 code to demonstrate working of
1_______0_______3
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
# Python3 code to demonstrate working of
4 # Python3 code to demonstrate working of
5
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
# Python3 code to demonstrate working of
8# Python3 code to demonstrate working of
9# Last business day
0# Python3 code to demonstrate working of
1
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
# Last business day
3# Last business day
4 # Python3 code to demonstrate working of
5
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
# Last business day
8# Python3 code to demonstrate working of
9# Last business day
0# Python3 code to demonstrate working of
1
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
# using timedelta[] + conditional statements + weekday[]
3# using timedelta[] + conditional statements + weekday[]
4 # Python3 code to demonstrate working of
9
# Last business day
0# Python3 code to demonstrate working of
1
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
from
0
from
1
from
2
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
from
5 from
6The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
from
8The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
datetime
0
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:003
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:004
datetime
3 The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:007
datetime
6đầu ra
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:00
Phương pháp 2. Sử dụng max[] + % toán tử + timedelta[]
Thực hiện các tác vụ theo cách tương tự, chỉ khác là phương thức tính toán các thay đổi chênh lệch để nhận toán tử max[] và %.
Python3
# Python3 code to demonstrate working of
# Last business day
datetime
9
from
datetime
import
datetime, timedelta
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:001
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:004_______0_______5
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:001
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:002
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:003
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:004
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:005
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:007
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:008
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
# Python3 code to demonstrate working of
0
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:0005
# Python3 code to demonstrate working of
1_______0_______3
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:0008
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:004_______0_______7
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:0011
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
# Last business day
8_______10_______0The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:0015
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:0016
from
5 # Last business day
3The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
from
0
from
1
from
2
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
from
5 from
6The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:003
from
8The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:000
datetime
0
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:003
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:004
datetime
3 The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:007
datetime
6đầu ra
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:00
Phương pháp 3. sử dụng pd. hàng loạt. bù đắp. Ngày làm việc[n]
Trong trường hợp này, chúng tôi tạo ngày làm việc bù trừ 1 ngày và trừ đi ngày khởi tạo. Điều này trả về ngày làm việc trước đó như mong muốn