Tôi đang đặt một var datetime như vậy:
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
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 timedelta
Để tăng thêm 500 ms:
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
Bạn có thể sử dụng nó để tăng số phút, giờ, ngày, v.v. Tài liệu:
//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:
timedelta[microseconds=1000] #1 milli second
Đã 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.
- %X
- Đại diện thời gian ư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
from
datetime
0fulldate = 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]
3fulldate = 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]
9fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"] fulldate = fulldate + datetime.timedelta[milliseconds=500]
3fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"] fulldate = fulldate + datetime.timedelta[milliseconds=500]
1timedelta[microseconds=1000] #1 milli second
2timedelta[microseconds=1000] #1 milli second
3fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"] fulldate = fulldate + datetime.timedelta[milliseconds=500]
4timedelta[microseconds=1000] #1 milli second
9fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"] fulldate = fulldate + datetime.timedelta[milliseconds=500]
6timedelta[microseconds=1000] #1 milli second
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]
0
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
timedelta[microseconds=1000] #1 milli second
Ví 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
date_object = 2021-06-15 13:30:15.120000
1
date_object = 2021-06-15 13:30:15.120000
2
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
Ví dụ 3: Thời gian với mili giây
2021-08-01 15:27:59.979673
5fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
3
2021-08-01 15:27:59.979673
7
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
Python mili giây cho đến nay với các ví dụ về mã
Xin chào tất cả mọi người, trong bài đăng này, chúng tôi sẽ điều tra cách giải câu đố Python Milliseconds cho đến nay bằng cách sử dụng ngôn ngữ lập trình.
Chúng tôi đã có thể hiểu làm thế nào để sửa chữa vấn đề Python mili giây cho đến nay nhờ nhiều ví dụ.
Làm thế nào để bạn chuyển đổi mili giây thành ngày?
Sử dụng hàm tạo ngày [] để chuyển đổi mili giây thành ngày, ví dụ: const ngày = ngày mới [dấu thời gian]. Hàm xây dựng ngày [] lấy giá trị số nguyên đại diện cho số mili giây kể từ ngày 1 tháng 1 năm 1970, 00:00:00 UTC và trả về một đối tượng ngày.25-tháng 1-2022
Làm thế nào để bạn đại diện cho mili giây ở định dạng ngày ở Python?
“Định dạng ngày Python với mili giây
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc function in python converts the string into DateTime objects. The strptime[] is a class method that takes two arguments :
- Bàn luận
- hàm strptime [] trong python chuyển đổi chuỗi thành các đối tượng DateTime. Strptime [] là một phương thức lớp có hai đối số: & nbsp;
Chuỗi nên được chuyển đổi thành đối tượng DateTime
Syntax:
Chuỗi định dạng được sử dụng để phân tích chuỗi.
Hai đối số chuỗi này là bắt buộc để chuyển đổi một chuỗi thành đối tượng DateTime.
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]
7date_object = 2021-06-15 13:30:15.1200006
fulldate = datetime.datetime.strptime[date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f"]
fulldate = fulldate + datetime.timedelta[milliseconds=500]
5date_object = 2021-06-15 13:30:15.1200008
date_object = 2021-06-15 13:30:15.1200009
2021-08-01 15:27:59.9796730
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]
42021-08-01 15:27:59.9796738
2021-08-01 15:27:59.9796739
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