Dấu thời gian là một chuỗi các ký tự hoặc thông tin được mã hóa được sử dụng để tìm thấy khi một sự kiện cụ thể xảy ra, thường đưa ra ngày và thời gian trong ngày, chính xác đến một phần nhỏ của một giây. Trong bài viết này, chúng tôi sẽ học cách có được dấu thời gian hiện tại trong Python.Get current timestamp in Python.
Có nhiều cách khác nhau để có được dấu thời gian hiện tại trong Python, chúng ta có thể sử dụng các chức năng từ các mô -đun thời gian, DateTime và lịch.
1. Sử dụng thời gian mô-đun: Mô-đun thời gian cung cấp các chức năng liên quan đến thời gian khác nhau. Thời gian chức năng, trả về thời gian tính bằng giây kể từ kỷ nguyên dưới dạng số điểm nổi. Epoch được định nghĩa là điểm mà thời gian bắt đầu và phụ thuộc vào nền tảng.
The
time module provides various time-related functions. The function time, return the time in seconds since the epoch as a floating point number. epoch is defined as the point where the time starts and is platform dependent.
Syntax: time.time[] Parameters: NA Return: floating point number expressed in seconds.
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.3
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.4
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.5
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.6
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.7
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.8
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.9
Output:
1594819641.9622827
& nbsp; 2. Sử dụng mô -đun DateTime: Mô -đun DateTime cung cấp các lớp để thao tác ngày và thời gian. Trong khi số học ngày và thời gian được hỗ trợ, mục tiêu của việc triển khai là về trích xuất thuộc tính hiệu quả để định dạng và thao tác đầu ra. Hàm dateTime.dateTime.now trả về số giây kể từ kỷ nguyên.
2. Using module datetime :
The datetime module provides classes
for manipulating dates and times.
While date and time arithmetic is supported, the target of the implementation is on efficient attribute extraction for output formatting and manipulation. The function datetime.datetime.now which return number of seconds since the epoch.
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.3
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594461
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594462
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.6
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594464
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.8
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594466
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594467
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594468
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.5
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.6
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.1
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.8
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594466
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.4
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.5
Output:
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.159446
& nbsp; 3. Sử dụng lịch mô -đun: Chúng tôi cũng có thể nhận dấu thời gian bằng cách kết hợp nhiều chức năng từ nhiều mô -đun. Trong điều này, chúng tôi sẽ sử dụng Lịch chức năng.Timegm để chuyển đổi Tuple đại diện cho thời gian hiện tại.
3. Using module calendar :
We can also get timestamp by combining multiple functions from multiple modules. In this we we’ll use function calendar.timegm to convert tuple representing current time.
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.3
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.7
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.3
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.9
calendar.timegm[tuple]
0Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.6
calendar.timegm[tuple]
2Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.8
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594466
calendar.timegm[tuple]
5calendar.timegm[tuple]
6Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.5
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.6
calendar.timegm[tuple]
9Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.8
current time:- 2020-07-15 14:30:26.159446 timestamp:- 1594823426.1594466
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.4
Syntax: calendar.timegm[tuple] Parameters: takes a time tuple such as returned by the gmtime[] function in the time module. Return: the corresponding Unix timestamp value.5
Output:
gmt:- thời gian.struct_time [tm_year = 2020, tm_mon = 7, tm_mday = 15, tm_hour = 19, tm_min = 21, tm_sec = 6, tm_wday = 2, tm_yday = 197, tm_isdst = 0] Times
timestamp:- 1594840866
Trong hướng dẫn ngắn này, chúng tôi nhìn vào dấu thời gian trong Python. Chúng tôi sẽ hiểu các cách khác nhau để có được dấu thời gian và chuyển đổi dấu thời gian cho đến ngày và thời gian.
Mục lục
- Dấu thời gian trong Python là gì?
- Cách để có được dấu thời gian hiện tại
- Sử dụng mô -đun lịch
- Sử dụng mô -đun thời gian
- Sử dụng mô -đun DateTime
- Chuyển đổi dấu thời gian cho đến ngày và thời gian
- Bớt tư tưởng
Dấu thời gian trong Python là gì?
Cách để có được dấu thời gian hiện tại The timestamp format in Python returns the time elapsed from the epoch time which is set to 00:00:00 UTC for 1 January 1970.
Sử dụng mô -đun lịch
Sử dụng mô -đun thời gian
- Sử dụng mô -đun DateTime
- Chuyển đổi dấu thời gian cho đến ngày và thời gian
- Bớt tư tưởng
Dấu thời gian là ngày và thời gian xảy ra của một sự kiện. Trong Python, chúng ta có thể có được dấu thời gian của một sự kiện với độ chính xác của mili giây. Định dạng dấu thời gian trong Python trả về thời gian trôi qua từ thời đại kỷ nguyên được đặt thành 00:00:00 UTC cho ngày 1 tháng 1 năm 1970.
Sử dụng mô -đun lịch
Sử dụng mô -đun thời gian
Syntax:
calendar.timegm[tuple]
Example:
import calendar
import time
current_GMT = time.gmtime[]
time_stamp = calendar.timegm[current_GMT]
print["Current timestamp:", time_stamp]
Output:
Current timestamp: 1647838478
Sử dụng mô -đun DateTime
Chuyển đổi dấu thời gian cho đến ngày và thời gian
Sử dụng mô -đun thời gian
Sử dụng mô -đun DateTime
Syntax:
time.time[]
Example:
import time
time_stamp = time.time[]
print["Timestamp:", time_stamp]
Output:
Chuyển đổi dấu thời gian cho đến ngày và thời gianBớt tư tưởng
Sử dụng mô -đun DateTime
Chuyển đổi dấu thời gian cho đến ngày và thời gian
Syntax:
1594819641.96228271
Example:
1594819641.96228272
Output:
1594819641.96228270
Bớt tư tưởng
Chuyển đổi dấu thời gian cho đến ngày và thời gian
Bớt tư tưởng
Syntax:
1594819641.96228274
Example:
1594819641.96228275
Output:
1594819641.96228276
Dấu thời gian là ngày và thời gian xảy ra của một sự kiện. Trong Python, chúng ta có thể có được dấu thời gian của một sự kiện với độ chính xác của mili giây. Định dạng dấu thời gian trong Python trả về thời gian trôi qua từ thời đại kỷ nguyên được đặt thành 00:00:00 UTC cho ngày 1 tháng 1 năm 1970.
Cách để có được dấu thời gian hiện tại trong Python
Example1:
1594819641.96228277
Output:
1594819641.96228278
Có ba mô -đun trong Python, sử dụng đó, chúng ta có thể có được dấu thời gian. Đó là-
Example2:
1594819641.96228279
Output:
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.0
lịch
Example3:
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.1
Output:
Syntax: datetime.now[] Parameters: tz [time zone] which is optional. Return: the current local date and time.2
thời gian
ngày giờ
Hãy cho chúng tôi xem các phương pháp này từng người một.