Trong bài viết này, chúng ta sẽ xem cách chuyển đổi DateTime Python thành Integer Timestamp. & NBSP;
Hàm dấu thời gian [] trả về thời gian được biểu thị bằng số giây đã trôi qua kể từ ngày 1 tháng 1 năm 1970. Khoảnh khắc bằng không được gọi là kỷ nguyên. Đầu tiên, chúng tôi sẽ nhận được thời gian hiện tại hoặc đề cập rõ ràng ngày và thời gian cần thiết mà chúng tôi muốn có dấu thời gian. Có một số cách để có được ngày và thời gian. Chúng ta sẽ thấy chúng khi chúng ta trải qua các ví dụ. Sau đó, chúng tôi sẽ chuyển đổi DateTime thành Timestamp bằng hàm Timestamp [].datetime into timestamp using timestamp[] function.
Cuối cùng, chúng tôi sẽ làm tròn dấu thời gian bằng vài giây và mili giây và đánh máy rõ ràng vào kiểu dữ liệu số nguyên và công việc của chúng tôi đã hoàn thành!
Ví dụ 1: Thời gian thời gian số nguyên của ngày và giờ hiện tại
Ở đây chúng tôi nhập mô -đun DateTime để sử dụng chức năng DateTime từ nó. Và sau đó sử dụng hàm dateTime.now [] để có được ngày và thời gian hiện tại. Chuyển đổi đối tượng DateTime thành dấu thời gian bằng phương thức DateTime.Timestamp []. Chúng tôi sẽ nhận được dấu thời gian trong vài giây. Và sau đó làm tròn dấu thời gian và đánh máy rõ ràng số điểm nổi vào một số nguyên để có được dấu thời gian số nguyên trong vài giây.
Python3
from
datetime
import
datetime
curr_dt
=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000000
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000003
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000004
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000005
=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000009
Timestamp in integer since epoch: 72600
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Timestamp in integer since epoch: 72603
Timestamp in integer since epoch: 72604
Timestamp in integer since epoch: 72605
Timestamp in integer since epoch: 72606
Output:
Current datetime: 2021-08-25 15:04:33.794484 Integer timestamp of current datetime: 1629884074
Ví dụ 2: & NBSP; Thời gian số nguyên của ngày và giờ được chỉ định
Cung cấp ngày và thời gian dưới dạng tham số bên trong hàm DateTime []. Chuyển đổi đối tượng DateTime thành dấu thời gian bằng phương thức DateTime.Timestamp []. Chúng tôi sẽ nhận được dấu thời gian trong vài giây. Làm tròn dấu thời gian và đánh máy rõ ràng số điểm nổi vào một số nguyên để có được dấu thời gian số nguyên trong giây. Chúng ta cũng có thể chuyển đổi nó thành mili giây bằng cách nhân nó BY1000 để có được dấu thời gian số nguyên tính bằng mili giây.
Python3
from
datetime
import
datetime
curr_dt
=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000000
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
from
4from
5Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000005
=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000009
Timestamp in integer since epoch: 72600
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
datetime
1Timestamp in integer since epoch: 72604
Timestamp in integer since epoch: 72605
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000009
datetime
7Ví dụ 2: & NBSP; Thời gian số nguyên của ngày và giờ được chỉ định
Cung cấp ngày và thời gian dưới dạng tham số bên trong hàm DateTime []. Chuyển đổi đối tượng DateTime thành dấu thời gian bằng phương thức DateTime.Timestamp []. Chúng tôi sẽ nhận được dấu thời gian trong vài giây. Làm tròn dấu thời gian và đánh máy rõ ràng số điểm nổi vào một số nguyên để có được dấu thời gian số nguyên trong giây. Chúng ta cũng có thể chuyển đổi nó thành mili giây bằng cách nhân nó BY1000 để có được dấu thời gian số nguyên tính bằng mili giây.
Timestamp in integer since epoch: 72605
datetime
2Output:
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 1514817000000
Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
1=
Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
3Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
4Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
5Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
6Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
5Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
6Time Zone: Asia/Kolkata
Datetime: 2021-08-25 15:09:05.194413+05:30
Integer timestamp: 1629884345
5from
0from
1
from
6=
from
8
Python3
datetime
8=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007__12
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
import
9Timestamp in integer since epoch: 72604
Ví dụ 3: UTC [Thời gian phổ biến] Thời gian số nguyên]
Đầu tiên, chúng ta nhập thời gian UTIC bên trong đối tượng DateTime.DateTime []. Sau đó, chúng tôi chuyển đối tượng cho hàm d.timtuple [] cung cấp một bộ chứa các tham số như năm, tháng, ngày, v.v., sau đó sử dụng hàm lịch, chúng tôi chuyển đổi DateTime thành dấu thời gian UTC số nguyên UTC.
import
datetime
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000000
Timestamp in integer since epoch: 72604
Timestamp in integer since epoch: 72605
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000003
Output:
Timestamp in integer since epoch: 7260
import
datetime
6
datetime
7=
datetime
9curr_dt
0
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843456
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843456
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455
curr_dt
6Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843456____35
=
0from
1Python3
datetime
8=
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007__12
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
import
9Timestamp in integer since epoch: 72604
Ví dụ 3: UTC [Thời gian phổ biến] Thời gian số nguyên]
Đầu tiên, chúng ta nhập thời gian UTIC bên trong đối tượng DateTime.DateTime []. Sau đó, chúng tôi chuyển đối tượng cho hàm d.timtuple [] cung cấp một bộ chứa các tham số như năm, tháng, ngày, v.v., sau đó sử dụng hàm lịch, chúng tôi chuyển đổi DateTime thành dấu thời gian UTC số nguyên UTC.
import
datetime
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000021
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000022
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
from
4Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000026
import
datetime
6
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000001
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000032
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000007
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000002
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 15148170000009
Datetime: 2018-01-01 20:00:00 Integer timestamp in seconds: 1514817000 Integer timestamp in milliseconds: 151481700000037
Output:
Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 1629884345