Hướng dẫn how do you add milliseconds to python? - làm thế nào để bạn thêm mili giây vào python?
Tôi đang đặt một var datetime như vậy: Show
trong đó ngày và thời gian là chuỗi có tính chất thích hợp cho DateTime. Làm thế nào tôi có thể tăng dữ liệu này theo n mili giây? Đã hỏi ngày 2 tháng 7 năm 2014 lúc 4:18Jul 2, 2014 at 4:18
Sử dụng Để tăng thêm 500 ms:
Bạn có thể sử dụng nó để tăng số phút, giờ, ngày, v.v. Tài liệu: https://docs.python.org/2/library/datetime.html#timedelta-objects Đã trả lời ngày 2 tháng 7 năm 2014 lúc 4:22Jul 2, 2014 at 4:22
Martin Konecnymartin KonecnyMartin Konecny 55,7K19 Huy hiệu vàng134 Huy hiệu bạc153 Huy hiệu đồng19 gold badges134 silver badges153 bronze badges Sử dụng Timedelta:
Đã trả lời ngày 2 tháng 7 năm 2014 lúc 4:23Jul 2, 2014 at 4:23
Venpavenpavenpa 4.18820 Huy hiệu bạc23 Huy hiệu đồng20 silver badges23 bronze badges 0 %W Số tuần của năm (thứ Hai là ngày đầu tiên trong tuần) là số thập phân. & NBSP; datetime.datetime.fromtimestamp(ms/1000.0) %c Đại diện ngày và thời gian ưa thích.Tue ngày 16 tháng 8 21:30:00 1998 %x& nbsp; đại diện ngày ưa thích.
%% - một nhân vật ‘%theo nghĩa đen.Để sử dụng chức năng này để sản xuất mili giây trong Python %F được sử dụng trong mã định dạng. Đưa ra dưới đây là một số triển khai.Ví dụ 1: Thời gian với mili giây
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f") fulldate = fulldate + datetime.timedelta(milliseconds=500) 5fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f") fulldate = fulldate + datetime.timedelta(milliseconds=500) 6timedelta(microseconds=1000) #1 milli second 9datetime.datetime.fromtimestamp(ms/1000.0)0Ví dụ 2: Thời gian với mili giây datetime.datetime.fromtimestamp(ms/1000.0)9fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 |
Strptime (Date_String, Format_String) | Danh sách mã định dạng: | Chuỗi định dạng |
---|---|---|
Diễn dịch | Thí dụWeekday as an abbreviated name. | %một |
& nbsp; ngày trong tuần là một tên viết tắt. | Mặt trời, Mon, xông, Sat & NBSP; | %MỘT |
Ngày trong tuần như tên đầy đủ. | Chủ nhật, thứ Hai,, Thứ Bảy & NBSP; | %w |
Ngày trong tuần là một số thập phân, 0 là Chủ nhật và 6 là thứ bảy. | 0, 1,…, 6 | %d |
Ngày trong tháng dưới dạng số thập phân số không. | 01, 02,…, 31 | %b |
Tháng như một tên viết tắt. | Jan, tháng 2,…, tháng 12 | %B |
Tháng. | Tháng 1, tháng 2,…, tháng 12 | %m |
Tháng | 01, 02,…, 12 | %y |
Năm không có thế kỷ. | 00, 01, xông, 99 | %Y |
Năm với thế kỷ. | 0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999 | %H |
Giờ (đồng hồ 24 giờ). | 00, 01,…, 23 | %m |
Tháng | 01, 02,…, 12 | %y |
Năm không có thế kỷ. | 00, 01, xông, 99 | %Y |
Năm với thế kỷ. | 0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999 | %Y |
Năm với thế kỷ. | 0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999Microsecond as a decimal number. | %H |
Giờ (đồng hồ 24 giờ). | 00, 01,…, 23 | %TÔI |
Giờ (đồng hồ 12 giờ). | %P | Hoặc là AM hoặc PM. |
Sáng, chiều | %M | Phút. |
00, 01,…, 59 | %S | Thứ hai. |
%f | & nbsp; microsecond dưới dạng số thập phân. | Thứ hai. |
%c | Đại diện ngày và thời gian ưa thích. | Tue ngày 16 tháng 8 21:30:00 1998 |
%x | & nbsp; đại diện ngày ưa thích.preferred date representation. | 08/16/88 08/16/1998 |
%X | Đại diện thời gian ưa thích. | 21:30:00 %% - một nhân vật ‘%theo nghĩa đen.A literal ‘%’ character. |
Để sử dụng chức năng này để sản xuất mili giây trong Python %F được sử dụng trong mã định dạng.
Đưa ra dưới đây là một số triển khai.
Ví dụ 1: Thời gian với mili giâyTime with milliseconds
Python3
from
datetime
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
0 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
1fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
2fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
4fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
5fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
6fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
7fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
8fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
9 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 timedelta(microseconds=1000) #1 milli second
1timedelta(microseconds=1000) #1 milli second
2fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 timedelta(microseconds=1000) #1 milli second
4fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
9timedelta(microseconds=1000) #1 milli second
6fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
5fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
6timedelta(microseconds=1000) #1 milli second
9datetime.datetime.fromtimestamp(ms/1000.0)0
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
5fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
6fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
7datetime.datetime.fromtimestamp(ms/1000.0)4
Output:
date_object = 2021-06-15 13:30:15.120000
Ví dụ 2: Thời gian với mili giâytime with milliseconds
Python3
from
datetime
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
0 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
1fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
2fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
4fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
5fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
6fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
76date_object = 2021-06-15 13:30:15.120000
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
58date_object = 2021-06-15 13:30:15.120000
9date_object = 2021-06-15 13:30:15.120000
02021-08-01 15:27:59.979673
Output:
2021-08-01 15:27:59.979673
9 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
1time with millisecondstimedelta(microseconds=1000) #1 milli second
Python3
from
datetime
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
0 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
1fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
2fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
3 fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
482021-08-01 15:27:59.979673
92021-08-01 15:27:59.979673
2021-10-17 15:48:35.5250010
2021-10-17 15:48:35.5250011
timedelta(microseconds=1000) #1 milli second
6fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)
52021-10-17 15:48:35.5250014
Output:
2021-10-17 15:48:35.525001