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:
Examples:
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
Các The original date is : 2017-03-14 Next date of required weekday : 2017-03-173 The original date is : 2017-03-14 Next date of required weekday : 2017-03-174 The original date is : 2017-03-14 Next date of required weekday : 2017-03-175 The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 The original date is : 2017-03-14 Next date of required weekday : 2017-03-177 The original date is : 2017-03-14 Next date of required weekday : 2017-03-178 The original date is : 2017-03-14 Next date of required weekday : 2017-03-179 import 0
The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 = datetime 8
The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 test_date 3
The original date is : 2017-03-14 Next date of required weekday : 2017-03-173 The original date is : 2017-03-14 Next date of required weekday : 2017-03-174 test_date 6 The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 The original date is : 2017-03-14 Next date of required weekday : 2017-03-177 test_date 9The original date is : 2017-03-14 Next date of required weekday : 2017-03-179 import 0Output: 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
Các The original date is : 2017-03-14 Next date of required weekday : 2017-03-173 The original date is : 2017-03-14 Next date of required weekday : 2017-03-174 The original date is : 2017-03-14 Next date of required weekday : 2017-03-175 The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 The original date is : 2017-03-14 Next date of required weekday : 2017-03-177 The original date is : 2017-03-14 Next date of required weekday : 2017-03-178 The original date is : 2017-03-14 Next date of required weekday : 2017-03-179 import 0
The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 = datetime 8The original date is : 2017-03-14 Next date of required weekday : 2017-03-173 The original date is : 2017-03-14 Next date of required weekday : 2017-03-174 test_date 6 The original date is : 2017-03-14 Next date of required weekday : 2017-03-176 The original date is : 2017-03-14 Next date of required weekday : 2017-03-177 test_date 9The original date is : 2017-03-14 Next date of required weekday : 2017-03-179 import 0Output: 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: a = now () delta = relaidaidingatetime (ngày =+6, ngày trong tuần (mx.datetime.friday, 0)) Thứ Sáu tới: A+Delta 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)) Đă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)) Đăng bởi VJ VJ, cả hai công việc đó, vì bạn vẫn còn hai lỗi chính tả. 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 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 Đă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? 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 () Đăng bởi VJ từ nhập mx.datetime *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 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 Hy vọng điều này sẽ giúp, 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. Cảm ơn Vj Đă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? Đă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 Nhập DateTime, Recurrecur.Recurrence (DateTime.Date.Today (), "Thứ Sáu"). Tiếp theo () DateTime.Date (2006, 3, 24) Loading... |