Python nhận được vào ngày trong tuần tới

Được đưa ra một số ngày và ngày duy nhất trong tuần, nhiệm vụ là viết một chương trình Python để có được ngày định trước nhất trong tuần xảy ra sau ngày định trước nhất. Chỉ số ngày trong tuần dựa trên bảng dưới đây

Mục lụcNgày trong tuần0Thứ hai1Thứ ba2Thứ tư3thứ năm4Thứ sáu5Thứ bảy6Chủ nhật

ví dụ

Đầu vào. test_date = ngày giờ. ngày giờ (2017, 3, 14), ngày trong tuần_idx = 4 test_date = ngày giờ. ngày giờ (2017, 3, 14), ngày trong tuần_idx = 4

đầu ra. 17-03-2017 & NBSP;

Giải thích. 14 tháng 3 là thứ ba, tức là 1 ngày trong tuần, ngày thứ 4 là thứ sáu, tức là 17 tháng 3. 14 tháng 3 là thứ ba, tôi. e 1 ngày trong tuần, ngày thứ 4 trong tuần là thứ sáu, i. ngày 17 tháng 3

Đầu vào. test_date = ngày giờ. ngày giờ (2017, 3, 12), tuần_idx = 5 test_date = ngày giờ. ngày giờ (2017, 3, 12), ngày trong tuần_idx = 5

đầu ra. 2017-03-18 2017-03-18

Giải thích. 12 tháng 3 là Chủ nhật, tức là ngày thứ 6, ngày thứ 5 trong tuần tới là thứ bảy, tức là 18 tháng 3. Ngày 12 tháng 3 là chủ nhật, tôi. e ngày thứ 6 trong tuần, ngày thứ 5 trong tuần tới là thứ bảy, tôi. ngày 18 tháng 3

Phương thức number 1. Use TimedelTa () + Day Day ()timedelta() + weekday()

Trong đó, chúng tôi trừ ngày trong tuần từ chỉ số ngày trong tuần và sau đó kiểm tra số chỉ cần thiết được trích xuất, sau đó là ngày cần thiết, nếu âm thanh được tính tổng với 7 và sau đó số kết quả được thêm vào

Python3

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
0

Các

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3______04
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
5
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
7
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
8
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
90

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
91
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
93

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
94
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
91
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
97
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
98

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
99
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
00
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
02
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
03

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
04
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
94

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
08

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
09
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
1

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
13
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
4
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
16
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
7
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
19
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
90

đầu ra

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17

Phương pháp số 2. Use the Lambdalmbda function

Chức năng sử dụng Lambda cung cấp giải pháp tốc ký và thu gọn cho câu hỏi.  

Python3

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
0

Các

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3______04
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
5
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
7
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
8
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
90

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
91
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
93

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
94
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
91
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
97
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
98

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
99
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
00
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
02
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
03

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
04
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
94

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
08
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
3
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
4
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
16
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
6______07
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
19
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
90

đầu ra

The original date is : 2017-03-14
Next date of required weekday : 2017-03-17

Thảo luận

Robert Brewersystem Kiến trúc sư Ministryfumanchu tại Amor. tổ chức

Cách tìm hiểu vào Thứ sáu tới bằng cách sử dụng tương đối

Tôi đang làm.
delta = ReltaiveDateTime(ngày=+6, ngày trong tuần(mx. Ngày giờ. Thứ Sáu, 0))
Thứ Sáu tới. a+delta

a = now () delta = relaidaidingatetime (ngày =+6, ngày trong tuần (mx. ngày giờ. thứ sáu, 0)) Thứ sáu tới. A+Delta
a+delta. Cho tôi ngày 31 tháng 3 chứ không phải ngày 24 tháng 3

A. 23 tháng 3+Delta. Cho tôi ngày 31 tháng 3 chứ không phải ngày 24 tháng 3

Ý tưởng bất kỳ?

Tôi đã tìm ra cách làm điều đó. Điều này không hoạt động

delta = relaidaidingatetime (ngày = 0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0))

But this thing active

Delta = relaidaidingatetime (ngày =+0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0))
delta = ReltaiveDateTime(days=0, weekday(mx. Ngày giờ. Thứ sáu, 0))
delta = ReltaiveDateTime(days=+0, weekday(mx. Ngày giờ. Thứ Sáu, 0))

Đăng bởi vjdelta = relaicedatetime (ngày = 0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0)) delta = relaicedatetime (ngày =+0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0))
Thứ hai, hoàn toàn không có sự khác biệt trong Python giữa 0 và +0.
VUI LÒNG đăng mã thực tế (cộng với kết quả chạy mã), như thế này

Đăng bởi VJ

VJ, cả hai công việc đó, vì bạn vẫn còn hai lỗi chính tả.
delta3=RelativeDateTime(days=+0, weekday=(Saturday, 0))
delta2

Delta = relaidaidatetime (ngày =+0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0))

Delta = relaidaidatetime (ngày =+0, ngày trong tuần (mx. ngày giờ. thứ sáu, 0))

Đăng bởi VJ

Delta = RelativingAtetime (ngày = 6, ngày trong tuần = (Thứ Bảy, 0)) A + Delta

Delta = RelativingAtetime (ngày trong tuần = (Thứ Bảy, 0)) A + Delta

Đăng bởi VJ

Hy vọng điều này sẽ giúp ích, John

TrueCheers, John
Chúc mừng,
John

Cảm ơn vì đã chỉ ra rằng ngày =+0 là không cần thiết. Các điểm khác nhau của bạn được ghi nhận tốt

Vj
a = now()
delta = ReltaiveDateTime(days=+6, weekday(mx. Ngày giờ. Thứ Sáu, 0))
Thứ Sáu tới. a+delta
a. ngày 23 tháng 3
a+delta. Cho tôi ngày 31 tháng 3 chứ không phải ngày 24 tháng 3
Bạn có ý kiến ​​gì không?

Đăng bởi vja = now () delta = relaicedatetime (ngày =+6, ngày trong tuần (mx. ngày giờ. thứ sáu, 0)) Thứ Sáu tới. A+Delta. 23a+Đồng bằng. Cho tôi ngày 31 tháng 3 và không phải là ngày 24 tháng 3?
Không thêm 6. Có vẻ như bạn đang nhảy qua
thứ 24 và sau đó chọn ngày thứ Sáu đầu tiên sau kết quả của
nhảy.

Chỉ là một ý tưởng sai nhịp. Use DateTime of Python instead of MX. DATETIME và Mô-đun tái sinh của tôi. http. //dự án. tình yêu. org/misc/wiki/recur