Hướng dẫn python datetime to unix timestamp integer - python datetime thành số nguyên dấu thời gian unix

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:  1514817000000
0

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
3
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
4

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
5=
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
9
Timestamp in integer since epoch: 7260
0

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Timestamp in integer since epoch: 7260
3
Timestamp in integer since epoch: 7260
4

Timestamp in integer since epoch: 7260
5
Timestamp in integer since epoch: 7260
6

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:  1514817000000
0

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2from4from5

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
5=
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
9
Timestamp in integer since epoch: 7260
0

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2datetime 1
Timestamp in integer since epoch: 7260
4

Timestamp in integer since epoch: 7260
5
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
9datetime 7

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.

Timestamp in integer since epoch: 7260
5datetime2

Output:

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: 16298843451= Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843453Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843454Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843456Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843456Time Zone: Asia/Kolkata Datetime: 2021-08-25 15:09:05.194413+05:30 Integer timestamp: 16298843455from0from1

from6= from8

Python3

datetime 8=

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7__12

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2import9
Timestamp in integer since epoch: 7260
4

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:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
00
Timestamp in integer since epoch: 7260
4

Timestamp in integer since epoch: 7260
5
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
03

Output:

Timestamp in integer since epoch: 7260

import datetime6

datetime7= datetime9curr_dt 0

Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
5
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
6
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
5
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
6
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
5curr_dt 6
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
5
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
6____35=0from1

Python3

datetime 8=

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7__12

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2import9
Timestamp in integer since epoch: 7260
4

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:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
21
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
22

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2from4
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
26

import datetime6

Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
1
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
32
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
5
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
7
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
2
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
9
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
37

Output:

Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345