91
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi muốn tìm ra DateTime 10 phút sau thời gian hiện tại. Giả sử chúng tôi có
from datetime import datetime
now = datetime.now[]
new_now = datetime.strptime[now, '%a, %d %b %Y %H:%M:%S %Z']
Tôi muốn tìm điều này ____10 và
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
1 sau 10 phút sau. Làm thế nào tôi có thể làm điều đó?
Ivanleoncz
8.0874 Huy hiệu vàng53 Huy hiệu bạc48 Huy hiệu đồng4 gold badges53 silver badges48 bronze badges
Đã hỏi ngày 1 tháng 6 năm 2011 lúc 17:43Jun 1, 2011 at 17:43
0
Đây là một bản sao của câu hỏi này. Về cơ bản, bạn chỉ cần thêm thời gian 10 phút để có được thời gian bạn muốn.
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
JDOAKTOWN
3.9436 huy hiệu vàng37 Huy hiệu bạc49 Huy hiệu đồng6 gold badges37 silver badges49 bronze badges
Đã trả lời ngày 1 tháng 6 năm 2011 lúc 17:51Jun 1, 2011 at 17:51
Efalconerefalconerefalconer
2.4711 Huy hiệu vàng17 Huy hiệu bạc10 Huy hiệu đồng1 gold badge17 silver badges10 bronze badges
4
now_plus_10m = now + datetime.timedelta[minutes = 10]
Xem các đối số bạn có thể chuyển trong tài liệu cho Timedelta.
istruble
Huy hiệu vàng 13K249 Huy hiệu bạc51 Huy hiệu đồng2 gold badges49 silver badges51 bronze badges
Đã trả lời ngày 1 tháng 6 năm 2011 lúc 17:52Jun 1, 2011 at 17:52
AkashakashAkash
2812 Huy hiệu bạc11 Huy hiệu đồng2 silver badges11 bronze badges
0
Tôi sẽ thêm 600 vào
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
2>>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
Đã trả lời ngày 1 tháng 6 năm 2011 lúc 17:51Jun 1, 2011 at 17:51
EfalconerefalconertMC
2.4711 Huy hiệu vàng17 Huy hiệu bạc10 Huy hiệu đồng11 gold badges59 silver badges96 bronze badges
0
Thêm phút vào DateTime trong Python #
Sử dụng lớp
3 từ mô -đun from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 để thêm phút vào DateTime, ví dụ: from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
5. Lớp from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
6 có thể được truyền một đối số from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
7 và thêm số phút được chỉ định vào DateTime.from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
Copied!
from datetime import datetime, timedelta # ✅ parse datetime string and add minutes to datetime d = '2023-11-24 09:30:00.000123' # 👇️ convert string to datetime object dt = datetime.strptime[d, '%Y-%m-%d %H:%M:%S.%f'] print[dt] # 👉️ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta[minutes=29] print[result_1] # 👉️ 2023-11-24 09:59:00.000123 # ----------------------- # ✅ add minutes to datetime dt_2 = datetime[2023, 9, 24, 9, 30, 35] print[dt_2] # 👉️ 2023-09-24 09:30:35 result_2 = dt_2 + timedelta[minutes=15] print[result_2] # 👉️ 2023-09-24 09:45:35 # ------------------------ # ✅ add minutes to current time now = datetime.today[] print[now] # 👉️ 2022-06-22 12:35:30.302272 result_3 = now + timedelta[minutes=5] print[result_3] # 👉️ 2022-06-22 12:40:30.302272
Nếu bạn chỉ có thành phần thời gian, ví dụ:
8 Cuộn xuống đoạn mã cuối cùng.from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
Đảm bảo nhập các lớp
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 và Timedelta từ mô -đun from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4.Ví dụ đầu tiên tạo ra một đối tượng
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 từ chuỗi DateTime và thêm phút vào nó.Copied!
from datetime import datetime, timedelta # ✅ parse datetime string and add minutes to datetime d = '2023-11-24 09:30:00.000123' # 👇️ convert string to datetime object dt = datetime.strptime[d, '%Y-%m-%d %H:%M:%S.%f'] print[dt] # 👉️ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta[minutes=29] print[result_1] # 👉️ 2023-11-24 09:59:00.000123
Phương thức DateTime.strptime [] trả về một đối tượng DateTime tương ứng với chuỗi ngày được cung cấp, được phân tích cú pháp theo định dạng.
Nếu bạn có một chuỗi ngày được định dạng theo một cách khác, hãy sử dụng bảng này của các tài liệu để tra cứu mã định dạng, bạn nên chuyển làm đối số thứ hai cho phương thức
now_plus_10m = now + datetime.timedelta[minutes = 10]
2.Ví dụ thứ hai sử dụng lớp DateTime để tạo đối tượng
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 và lớp from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
6 để thêm phút cho nó.Copied!
from datetime import datetime, timedelta dt_2 = datetime[2023, 9, 24, 9, 30, 35] print[dt_2] # 👉️ 2023-09-24 09:30:35 result_2 = dt_2 + timedelta[minutes=15] print[result_2] # 👉️ 2023-09-24 09:45:35
Chúng tôi đã vượt qua các giá trị cho
now_plus_10m = now + datetime.timedelta[minutes = 10]
5, now_plus_10m = now + datetime.timedelta[minutes = 10]
6, now_plus_10m = now + datetime.timedelta[minutes = 10]
7, now_plus_10m = now + datetime.timedelta[minutes = 10]
8, now_plus_10m = now + datetime.timedelta[minutes = 10]
9 và >>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
0 đối số.Ví dụ thứ ba thêm phút cho thời gian hiện tại.
Copied!
from datetime import datetime, timedelta now = datetime.today[] print[now] # 👉️ 2022-06-22 12:35:30.302272 result_3 = now + timedelta[minutes=5] print[result_3] # 👉️ 2022-06-22 12:40:30.302272
Phương thức DateTime.today [] trả về DateTime cục bộ hiện tại.
Chúng ta cần sử dụng một đối tượng
4 vì nó tự động lăn trong các giờ, ngày, tháng và năm nếu cần thiết.from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
Điều này sẽ không thể xảy ra nếu chúng ta chỉ có thành phần thời gian. Ví dụ,
>>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
2 + >>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
3 phút sẽ tăng một ngoại lệ.Nếu bạn chỉ có thành phần thời gian, hãy sử dụng phương thức
4 để kết hợp thời gian với ngày [hoặc một số khác] và nhận được đối tượng >>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
4.from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
Copied!
from datetime import datetime, date, timedelta, time t = time[9, 25] print[t] # 👉️ 09:25:00 result = datetime.combine[date.today[], t] + timedelta[minutes=30] print[result] # 👉️ 2022-06-22 09:55:00 only_t = result.time[] print[only_t] # 👉️ 09:55:00
Phương thức datetime.combine lấy
>>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
6 và >>> import time
>>> print time.ctime[]
Wed Jun 1 13:49:09 2011
>>> future = time.time[] + 600
>>> print time.ctime[future]
Wed Jun 1 13:59:15 2011
>>>
7 làm đối số và trả về một đối tượng from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 mới bằng cách kết hợp chúng.Khi chúng ta nhận được một đối tượng
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4, chúng ta có thể sử dụng lớp from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
6 để thêm phút cho nó.Sử dụng phương thức
1 trên đối tượngCopied!
from datetime import datetime, timedelta # ✅ parse datetime string and add minutes to datetime d = '2023-11-24 09:30:00.000123' # 👇️ convert string to datetime object dt = datetime.strptime[d, '%Y-%m-%d %H:%M:%S.%f'] print[dt] # 👉️ 2023-11-24 09:30:00.000123 result_1 = dt + timedelta[minutes=29] print[result_1] # 👉️ 2023-11-24 09:59:00.000123 # ----------------------- # ✅ add minutes to datetime dt_2 = datetime[2023, 9, 24, 9, 30, 35] print[dt_2] # 👉️ 2023-09-24 09:30:35 result_2 = dt_2 + timedelta[minutes=15] print[result_2] # 👉️ 2023-09-24 09:45:35 # ------------------------ # ✅ add minutes to current time now = datetime.today[] print[now] # 👉️ 2022-06-22 12:35:30.302272 result_3 = now + timedelta[minutes=5] print[result_3] # 👉️ 2022-06-22 12:40:30.302272
from datetime import datetime, timedelta
now = datetime.now[]
now_plus_10 = now + timedelta[minutes = 10]
4 nếu bạn chỉ cần trích xuất thời gian sau khi hoạt động.Copied!
from datetime import datetime, date, timedelta, time t = time[9, 25] print[t] # 👉️ 09:25:00 result = datetime.combine[date.today[], t] + timedelta[minutes=30] print[result] # 👉️ 2022-06-22 09:55:00 # ✅ only get updated time only_t = result.time[] print[only_t] # 👉️ 09:55:00
Phương thức DateTime.time trả về một đối tượng thời gian với cùng một giờ, phút, thứ hai và mili giây.