Hướng dẫn how do you get next friday in python? - làm thế nào để bạn có được thứ sáu tới trong python?

Cải thiện bài viết

Lưu bài viết

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

Mục lụcNgày trong tuần
0 Thứ hai
1 Thứ ba
2 Thứ Tư
3 thứ năm
4 Thứ sáu
5 Thứ bảy
6 Chủ nhật

Examples:

Đầu vào: test_date = datetime.datetime (2017, 3, 14), weekday_idx = 4 test_date = datetime.datetime(2017, 3, 14), weekday_idx = 4

Đầu ra: 2017-03-17 & NBSP; 2017-03-17 

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 March is Tuesday, i.e 1 weekday, 4th weekday is a Friday, i.e 17 March.

Đầu vào: test_date = datetime.datetime (2017, 3, 12), weekay_idx = 5 test_date = datetime.datetime(2017, 3, 12), weekday_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. 12 March is Sunday, i.e 6th weekday, 5th weekday in next week is a Saturday, i.e 18 March.

Phương thức số 1: Sử dụng 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 chỉ số cần thiết được trích xuất, sau đó là ngày cần thiết, nếu âm được tính tổng với 7 và sau đó số kết quả được thêm vào ngày hiện tại bằng thời gian sử dụng Timedelta ().

Python3

import datetime

Các

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
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
9import0

import1= import3

import4= import1import7 import8

import9 datetime0= datetime2datetime3

datetime4import4

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

datetime9= test_date

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

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
4test_date 6
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
7test_date 9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9import0

Output:

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

Phương pháp số 2: Sử dụng chức năng Lambdalambda function

Sử dụng chức năng Lambda cung cấp một giải pháp tốc ký và nhỏ gọn cho câu hỏi. & NBSP;

Python3

import datetime

Các

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
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
9import0

import1= import3

import4= import1import7 import8

import9 datetime0= datetime2datetime3

datetime4import4

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

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
4test_date 6
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
7test_date 9
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
9import0

Output:

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

Discussion:

Robert Brewersystem Architector Ministryfumanchu tại Amor.org

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(days=+6, weekday(mx.DateTime.Friday, 0))
Next Friday: a+delta

a = now () delta = relaidaidingatetime (ngày =+6, ngày trong tuần (mx.datetime.friday, 0)) Thứ Sáu tới: A+Delta
a+delta: Gives me March 31st and not March 24th

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

Bất kỳ ý tưởng?

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.datetime.friday, 0))

Nhưng điều này hoạt động:

Delta = relaidaidingatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 0))
delta = ReltaiveDateTime(days=0, weekday(mx.DateTime.Friday, 0))
delta = ReltaiveDateTime(days=+0, weekday(mx.DateTime.Friday, 0))

Đăng bởi vjdelta = relaicedatetime (ngày = 0, ngày trong tuần (mx.datetime.friday, 0)) delta = relaicedatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 0))
Secondly, there is absolutely no difference in Python between 0 and +0.
PLEASE post actual code (plus the results of running it), like this

Đă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 = relaidaidingatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 0))

Delta = relaidaidingatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 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, John

TrueCheers,John
Cheers,
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 của bạn được ghi nhận tốt.

Vj
a = now()
delta = ReltaiveDateTime(days=+6, weekday(mx.DateTime.Friday, 0))
Next Friday: a+delta
a: march 23
a+delta: Gives me March 31st and not March 24th
Any ideas?

Đăng bởi vja = now () delta = relaicedatetime (ngày =+6, ngày trong tuần (mx.datetime.friday, 0)) Thứ Sáu tới: A+Deltaa: 23a+Delta: Cho tôi ngày 31 tháng 3 và không phải là ngày 24 tháng 3?
Don't add the 6... It would appear that you are jumping over the
24th, and then picking up the first Friday that follows the result of
jumping.

Chỉ là một ý tưởng không đúng nhịp: Sử dụng DateTime của Python thay vì MX.DATETIME và Mô-đun tái hiện của tôi: http: //projects.amor.org/misc/wiki/recur

Nhập DateTime, Recurrecur.Recurrence (DateTime.Date.Today (), "Thứ Sáu"). Tiếp theo ()
code that you had actually run, rather than typing it from memory. There
are TWO typos in what your wrote. It would also help if you showed
exactly what import statement(s) were used, as in the following (where I
have substituted Saturday for Friday, in the interests of reproducing
your (volatile) test case).

Đăng bởi VJ

từ nhập mx.datetime *a = now () a
a = now()
a

Delta = relaidaidingatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 0))

Đăng bởi VJ

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

Delta = relaidaidingatetime (ngày =+0, ngày trong tuần (mx.datetime.friday, 0))

Đăng bởi VJ

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

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

Hy vọng điều này sẽ giúp, John
points are well noted.

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

Cảm ơn

Vj
a = now()
delta = ReltaiveDateTime(days=+6, weekday(mx.DateTime.Friday, 0))
Next Friday: a+delta
a: march 23
a+delta: Gives me March 31st and not March 24th
Any ideas?

Đăng bởi vja = now () delta = relaicedatetime (ngày =+6, ngày trong tuần (mx.datetime.friday, 0)) Thứ Sáu tới: A+Deltaa: 23a+Delta: Cho tôi ngày 31 tháng 3 và không phải là ngày 24 tháng 3?
and my recur module:
http://projects.amor.org/misc/wiki/Recur

Đăng bởi VJ

Chỉ là một ý tưởng không đúng nhịp: Sử dụng DateTime của Python thay vì MX.DATETIME và Mô-đun tái hiện của tôi: http: //projects.amor.org/misc/wiki/recur
recur.Recurrence(datetime.date.today(), "Friday").next()

Nhập DateTime, Recurrecur.Recurrence (DateTime.Date.Today (), "Thứ Sáu"). Tiếp theo ()

DateTime.Date (2006, 3, 24)
System Architect
Amor Ministries
fumanchu at amor.org

Loading...