Hướng dẫn how do i convert a date to a timestamp in python? - làm cách nào để chuyển đổi ngày thành dấu thời gian trong python?

Rất nhiều câu trả lời này không bận tâm xem xét rằng ngày là ngây thơ để bắt đầu

Show

Để đúng, bạn cần làm cho ngày ngây thơ trở thành một thời gian dữ liệu nhận thức về thời gian

import datetime
import pytz
# naive datetime
d = datetime.datetime.strptime('01/12/2011', '%d/%m/%Y')
>>> datetime.datetime(2011, 12, 1, 0, 0)

# add proper timezone
pst = pytz.timezone('America/Los_Angeles')
d = pst.localize(d)
>>> datetime.datetime(2011, 12, 1, 0, 0,
tzinfo=)

# convert to UTC timezone
utc = pytz.UTC
d = d.astimezone(utc)
>>> datetime.datetime(2011, 12, 1, 8, 0, tzinfo=)

# epoch is the beginning of time in the UTC timestamp world
epoch = datetime.datetime(1970,1,1,0,0,0,tzinfo=pytz.UTC)
>>> datetime.datetime(1970, 1, 1, 0, 0, tzinfo=)

# get the total second difference
ts = (d - epoch).total_seconds()
>>> 1322726400.0

Also:

Hãy cẩn thận, sử dụng pytz cho

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
0 trong
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
1 không hoạt động cho nhiều múi giờ. Xem DateTime với thời gian pytz. Bù khác nhau tùy thuộc vào cách TZINFO được đặt

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset

https://en.wikipedia.org/wiki/Local_mean_time

Dấu thời gian UNIX là một số nguyên có chữ ký duy nhất tăng lên mỗi giây, cho phép máy tính lưu trữ và thao tác các hệ thống ngày thông thường. Phần mềm sau đó được dịch thành định dạng có thể đọc được của con người. Dấu thời gian UNIX là số giây được tính toán kể từ ngày 1 tháng 1 năm 1970. Trong bài viết này, chúng ta sẽ xem cách chuyển đổi DateTime thành Unix Timestamp.

DateTime to Unix TimeStamp

Để chuyển đổi DateTime Python thành Unix Timestamp, chúng tôi đã nhập một mô -đun gọi là DateTime và Time trong ví dụ này và biến DATE_TIME đã được khai báo và gán DateTime. Thời gian/ngày (2021, 7, 26, 21, 20). Năm là 2021, tháng là 7, ngày là 26, giờ là 21 và phút là 20.

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
5

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
6
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
8
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
9
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
1
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
3______

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497724509.293
0
1628497724509.293
1
1628497724509.293
2

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497724509.293
0
1628497724509.293
5
1628497724509.293
6

1628497724509.293
7
1628497724509.293
8

Output:

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0

Explanation:

Các lớp thao tác ngày và giờ được cung cấp bởi mô -đun DateTime. Hàm nghịch đảo của thời gian địa phương là mktime (). Nó chấp nhận thời gian cấu trúc hoặc 9 tuple đầy đủ như một đối số và trả về một số điểm nổi để tương thích với thời gian (). Nó cũng được sử dụng để chuyển đổi DateTime thành dấu thời gian UNIX.

Phương thức thời gian biểu () của các đối tượng DateTime.date trả về một đối tượng thời gian. Thời gian. Đối tượng thời gian cấu trúc là một bộ tuple có tên có thể được truy xuất bằng cách sử dụng một chỉ mục hoặc theo tên. Các trường năm, tháng và ngày của bộ tuple được đặt tên được trả về bởi hàm Timetuple () sẽ được đặt theo đối tượng ngày, trong khi các trường, phút và giây sẽ được đặt thành 0.

DateTime to Unix TimeStamp với 13 chữ số

Để có được thời gian hiện tại, hãy sử dụng DateTime.now (). Hàm Timetuple () của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Dấu thời gian với 13 chữ số phải được nhân với 1000.

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
5

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
6
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
8
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
9
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
1
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
3______

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497724509.293
0
1628497724509.293
1
1628497724509.293
2

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497823000.0
2

Output:

1628497724509.293

Các lớp thao tác ngày và giờ được cung cấp bởi mô -đun DateTime. Hàm nghịch đảo của thời gian địa phương là mktime (). Nó chấp nhận thời gian cấu trúc hoặc 9 tuple đầy đủ như một đối số và trả về một số điểm nổi để tương thích với thời gian (). Nó cũng được sử dụng để chuyển đổi DateTime thành dấu thời gian UNIX.

Phương thức thời gian biểu () của các đối tượng DateTime.date trả về một đối tượng thời gian. Thời gian. Đối tượng thời gian cấu trúc là một bộ tuple có tên có thể được truy xuất bằng cách sử dụng một chỉ mục hoặc theo tên. Các trường năm, tháng và ngày của bộ tuple được đặt tên được trả về bởi hàm Timetuple () sẽ được đặt theo đối tượng ngày, trong khi các trường, phút và giây sẽ được đặt thành 0.

Code:

Python3

DateTime to Unix TimeStamp với 13 chữ số

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

Để có được thời gian hiện tại, hãy sử dụng DateTime.now (). Hàm Timetuple () của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Dấu thời gian với 13 chữ số phải được nhân với 1000.

1628497783
3
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
1628497783
5

1628497783
6
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
1628497783
8
1628497783
9
1628497823000.0
0

Output:

1628497783

DateTime to Unix Timestamp trong UTC Timezone

Mô-đun lịch cung cấp các chức năng liên quan đến lịch hữu ích. Hàm UTC.now trả về thời gian hiện tại trong múi giờ UTC. Trong mô -đun thời gian, hàm timegm trả về dấu thời gian UNIX. Hàm Timetuple () của lớp DateTime trả về các thuộc tính của DateTime dưới dạng một tuple có tên. Để có được dấu thời gian UNIX, hãy sử dụng in (UTC).

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
5

Unix_Time:  1628188200.0
9
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
1628497783
5

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628209448.0
3
1628497783
9
1628497823000.0
0
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
8

Output:

1628497823000.0

DateTime.Date to Unix Timestamp

Time.Date () là một hàm chấp nhận chỉ ngày. Trong trường hợp này, năm 2021 là năm, 8 là tháng và 6 là ngày. mktime () là một phương thức thời gian là hàm nghịch đảo của thời gian địa phương; Nó được sử dụng để chuyển đổi ngày thành dấu thời gian UNIX.

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
5

Unix_Time:  1628188200.0
9
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
1628497783
5

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497724509.293
0pytz2
1628497724509.293
6

1628497724509.293
7pytz5

Output:

Unix_Time:  1628188200.0

date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.091628209448.0316284977839 1628497823000.00date_time => 2021-07-26 21:20:00 unix_timestamp => 1627314600.08

DateTime.Date to Unix Timestamp

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

Time.Date () là một hàm chấp nhận chỉ ngày. Trong trường hợp này, năm 2021 là năm, 8 là tháng và 6 là ngày. mktime () là một phương thức thời gian là hàm nghịch đảo của thời gian địa phương; Nó được sử dụng để chuyển đổi ngày thành dấu thời gian UNIX.

Unix_Time => 1627334400
Date & Time => 2021-07-27 02:50:00
1
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
Unix_Time => 1627334400
Date & Time => 2021-07-27 02:50:00
3
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
9
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
Unix_Time => 1627334400
Date & Time => 2021-07-27 02:50:00
6
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
0
Unix_Time => 1627334400
Date & Time => 2021-07-27 02:50:00
8
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
8

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
04
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
05
date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
8

Chuỗi DateTime đến Unix TimeStamp

Ngày và thời gian được cung cấp ở định dạng chuỗi trong trường hợp này. Ở đây, 8 biểu thị tháng, 6 biểu thị ngày, 2021 biểu thị năm, 05 biểu thị giờ, 54 biểu thị phút và 8 biểu thị thứ hai. strptime () là một phương thức mô -đun DateTime được sử dụng để chuyển đổi chuỗi thành các đối tượng DateTime và Time. Hàm dấu thời gian () trả về thời gian hiện tại ở vị trí hiện tại.

Output:

1628209448.0

pytz8# Don't do this: d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles')) >>> datetime.datetime(2011, 1, 12, 0, 0, tzinfo=) # tzinfo in not PST but LMT here, with a 7min offset !!! # when converting to UTC: d = d.astimezone(pytz.UTC) >>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=) # you end up with an offset 7 # Don't do this: d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles')) >>> datetime.datetime(2011, 1, 12, 0, 0, tzinfo=) # tzinfo in not PST but LMT here, with a 7min offset !!! # when converting to UTC: d = d.astimezone(pytz.UTC) >>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=) # you end up with an offset 00

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
01
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
03

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
07
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
7
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
09

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
11

Code:

Python3

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
2
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
3

UNIX dấu thời gian đến Python DateTime

Mô-đun DateTime trong Python được sử dụng để xử lý các vấn đề ngày và thời gian trong Python. Phương thức Fromtimestamp () là một trong những hàm có trong mô -đun này. Chức năng của lớp ngày từ Fromtimestamp () tính toán và trả về ngày tương ứng với một dấu thời gian được chỉ định. Phạm vi dấu thời gian được phép kéo dài từ năm 1970 đến 2038. Nếu có bất kỳ giây bước nhảy nào trong dấu thời gian, hàm fromtimestamp () sẽ bỏ qua chúng.

date_time => 2021-07-26 21:20:00
unix_timestamp =>  1627314600.0
9
1628497724509.293
0
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
22
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
23

Để bắt đầu, chúng tôi nhập lớp DateTime từ mô -đun DateTime. Đối tượng giá trị Unix sau đó được lưu trữ trong một biến. Sau đó, chúng tôi sử dụng phương thức DateTime.FromTimeStamp () để truy xuất thời gian và ngày. & nbsp;

1628497724509.293
7
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
29
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
30
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=)
# you end up with an offset
31

Output:

Unix_Time => 1627334400
Date & Time => 2021-07-27 02:50:00

Làm cách nào để chuyển đổi dấu ngày theo thời gian trong Python?

Chúng ta có thể chuyển đổi một đối tượng DateTime thành dấu thời gian bằng phương thức Timestamp ().Nếu đối tượng DateTime được UTC biết, thì phương thức này sẽ tạo dấu thời gian UTC.Nếu đối tượng là ngây thơ, chúng ta có thể gán giá trị UTC cho tham số TZINFO của đối tượng DateTime và sau đó gọi phương thức Timestamp ().using the timestamp() method. If the datetime object is UTC aware, then this method will create a UTC timestamp. If the object is naive, we can assign the UTC value to the tzinfo parameter of the datetime object and then call the timestamp() method.

Làm cách nào để chuyển đổi một ngày trong Python?

Đối với điều này, chúng tôi sẽ sử dụng phương thức Strptime () và mô -đun pandas.Phương thức này được sử dụng để tạo một đối tượng DateTime từ một chuỗi.Sau đó, chúng tôi sẽ trích xuất ngày từ đối tượng DateTime bằng hàm ngày () và dt.Ngày từ Pandas ở Python.use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt. date from Pandas in Python.

Làm cách nào để định dạng dấu thời gian trong Python?

Một số chỉ thị hữu ích nhất là:..
%d cho ngày trong tháng dưới dạng số thập phân bằng không 01, 02, 03 ..
%m cho tháng dưới dạng số thập phân số không ..
%Y cho năm với thế kỷ là một số thập phân ..
%H cho đồng hồ 24 giờ với giá trị giờ bằng không ..
%M cho những phút không có viền và ..
%S cho những giây không có viền ..