Làm thế nào để tôi có được ngày thứ sáu trước đó trong python?

# 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 datetimeimport datetime, timedelta

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
1

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
4_______0_______5
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
4
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
5
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
7
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
8

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

# Python3 code to demonstrate working of0

# Python3 code to demonstrate working of1_______0_______3

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7

# Python3 code to demonstrate working of4 # Python3 code to demonstrate working of5

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3 # Python3 code to demonstrate working of8# Python3 code to demonstrate working of9

# Last business day0# Python3 code to demonstrate working of1

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3 # Last business day3

# Last business day4 # Python3 code to demonstrate working of5

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3 # Last business day8# Python3 code to demonstrate working of9

# Last business day0# Python3 code to demonstrate working of1

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3 # using timedelta[] + conditional statements + weekday[]3

# using timedelta[] + conditional statements + weekday[]4 # Python3 code to demonstrate working of9

# Last business day0# Python3 code to demonstrate working of1

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7

from0

from1

from2

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2from5 from6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3from8

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

datetime0

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
4datetime3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
7datetime6

đầ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

datetime9

from datetimeimport datetime, timedelta

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
1

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
4_______0_______5
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
4
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
5
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
7
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
8

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

# Python3 code to demonstrate working of0

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
05

# Python3 code to demonstrate working of1_______0_______3

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
08
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
4_______0_______7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
11
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6 # Last business day8_______10_______0
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
15
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
16 from5 # Last business day3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0

from0

from1

from2

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2from5 from6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
3from8

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0

datetime0

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
4datetime3
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
7datetime6

đầ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

Chủ Đề