print[f"date: {now.date}"]1tuân
print[f"year: {now.year}"]
print[f"month: {now.month}"]
print[f"day: {now.day}"]
print[f"time: {now.time}"]
print[f"hour: {now.hour}"]
print[f"minute: {now.minute}"]
print[f"second: {now.second}"]
print[f"microsecond: {now.microsecond}"]>>> date:
>>> year: 2022
>>> month: 7
>>> day: 18
>>> time:
>>> hour: 16
>>> minute: 50
>>> second: 24
>>> microsecond: 545047
print[f"date: {now.date}"]2theo các đại diện tương tự như danh sách mã ở trên, chẳng hạn như “%d /%m /%Y,%H. %M. %S”
print[f"year: {now.year}"]
print[f"month: {now.month}"]
print[f"day: {now.day}"]
print[f"time: {now.time}"]
print[f"hour: {now.hour}"]
print[f"minute: {now.minute}"]
print[f"second: {now.second}"]
print[f"microsecond: {now.microsecond}"]>>> date:
>>> year: 2022
>>> month: 7
>>> day: 18
>>> time:
>>> hour: 16
>>> minute: 50
>>> second: 24
>>> microsecond: 545047
Có một số cách chúng ta có thể thực hiện để có được ngày hiện tại. Chúng tôi sẽ sử dụng lớp date
của mô-đun datetime để hoàn thành nhiệm vụ này
ví dụ 1. Python lấy ngày hôm nay
from datetime import date
today = date.today[]
print["Today's date:", today]
đầu ra
Today's date: 2022-12-27
Ở đây, chúng tôi đã nhập lớp date
từ mô-đun datetime
. Sau đó, chúng tôi đã sử dụng phương pháp date.today[]
để lấy ngày địa phương hiện tại
ví dụ 2. Ngày hiện tại ở các định dạng khác nhau
from datetime import date
today = date.today[]
# dd/mm/YY
d1 = today.strftime["%d/%m/%Y"]
print["d1 =", d1]
# Textual month, day and year
d2 = today.strftime["%B %d, %Y"]
print["d2 =", d2]
# mm/dd/y
d3 = today.strftime["%m/%d/%y"]
print["d3 =", d3]
# Month abbreviation, day and year
d4 = today.strftime["%b-%d-%Y"]
print["d4 =", d4]
đầu ra
d1 = 27/12/2022 d2 = December 27, 2022 d3 = 12/27/22 d4 = Dec-27-2022
Nếu chúng ta cần lấy ngày giờ hiện tại, bạn có thể sử dụng lớp ________ 52 _______ của mô-đun ________ 52 _______
Trong bài viết này, bạn sẽ học cách sử dụng đối tượng datetime
từ mô-đun datetime
để lấy các thuộc tính ngày và giờ hiện tại
Bạn cũng sẽ học cách lấy ngày và giờ của các địa điểm khác nhau trên thế giới bằng cách sử dụng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 và mô-đun from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2Cách sử dụng đối tượng datetime
trong Python
Để sử dụng đối tượng datetime
, trước tiên bạn phải nhập nó. Đây là cách
Trong ví dụ tiếp theo, bạn sẽ thấy cách sử dụng đối tượng datetime
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
Trong đoạn mã trên, chúng tôi đã gán datetime
cho một biến có tên là
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
7Khi được in ra bàn điều khiển, chúng tôi có năm, tháng, ngày và giờ hiện tại.
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
8Lưu ý rằng chúng tôi có thể truy cập thông tin ở trên bằng phương pháp
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
9. from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1Cách sử dụng các thuộc tính from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
Trong phần trước, chúng tôi đã truy xuất thông tin về ngày và giờ hiện tại bao gồm năm, tháng, ngày và thời gian hiện tại tại thời điểm đó
Nhưng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 cung cấp cho chúng ta các thuộc tính bổ sung để trích xuất dữ liệu riêng lẻVí dụ: để chỉ lấy năm hiện tại, bạn sẽ làm điều gì đó như thế này
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime.year]
# 2022
Trong ví dụ trên, chúng ta đã gán hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 cho một biến có tên là from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
7Sử dụng ký hiệu dấu chấm, chúng tôi đã gắn thuộc tính
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime.year]
# 2022
5 cho biến được khai báo ở trên. from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime.year]
# 2022
6. Khi được in ra bảng điều khiển, chúng tôi có 2022Hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 có các thuộc tính sau
5from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022
9from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022
0from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022 print[current_dateTime.month] # 9 print[current_dateTime.day] # 20 print[current_dateTime.hour] # 11 print[current_dateTime.minute] # 27 print[current_dateTime.second] # 46 print[current_dateTime.microsecond] # 582035
1from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022 print[current_dateTime.month] # 9 print[current_dateTime.day] # 20 print[current_dateTime.hour] # 11 print[current_dateTime.minute] # 27 print[current_dateTime.second] # 46 print[current_dateTime.microsecond] # 582035
2from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022 print[current_dateTime.month] # 9 print[current_dateTime.day] # 20 print[current_dateTime.hour] # 11 print[current_dateTime.minute] # 27 print[current_dateTime.second] # 46 print[current_dateTime.microsecond] # 582035
3from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022 print[current_dateTime.month] # 9 print[current_dateTime.day] # 20 print[current_dateTime.hour] # 11 print[current_dateTime.minute] # 27 print[current_dateTime.second] # 46 print[current_dateTime.microsecond] # 582035
4from datetime import datetime current_dateTime = datetime.now[] print[current_dateTime.year] # 2022 print[current_dateTime.month] # 9 print[current_dateTime.day] # 20 print[current_dateTime.hour] # 11 print[current_dateTime.minute] # 27 print[current_dateTime.second] # 46 print[current_dateTime.microsecond] # 582035
Đây là một ví dụ về các thuộc tính được liệt kê ở trên được sử dụng
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime.year] # 2022
print[current_dateTime.month] # 9
print[current_dateTime.day] # 20
print[current_dateTime.hour] # 11
print[current_dateTime.minute] # 27
print[current_dateTime.second] # 46
print[current_dateTime.microsecond] # 582035
Cách lấy múi giờ cụ thể trong Python bằng cách sử dụng from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 và from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
Để lấy thông tin về các múi giờ khác nhau trên thế giới bằng Python, bạn có thể sử dụng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 và mô-đun from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2Đây là một ví dụ cho thấy cách lấy ngày và giờ hiện tại ở Lagos, Nigeria
from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
print[datetime_in_Lagos]
# 2022-09-20 12:53:27.225570+01:00
Trong đoạn mã trên, trước tiên chúng tôi đã nhập các mô-đun
from datetime import datetime
import pytz
Sau đó, chúng tôi đã chuyển đối tượng
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2 làm tham số cho hàm from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
Đối tượng
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2 có thuộc tính from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
print[datetime_in_Lagos]
# 2022-09-20 12:53:27.225570+01:00
2 lấy thông tin/tham số của múi giờ cụ thể mà bạn đang tìm kiếm. from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
print[datetime_in_Lagos]
# 2022-09-20 12:53:27.225570+01:00
3Với thuộc tính
from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
print[datetime_in_Lagos]
# 2022-09-20 12:53:27.225570+01:00
4, bạn có thể nhận danh sách tất cả các múi giờ có thể có trong thư viện from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2 có thể được chuyển vào dưới dạng tham số cho thuộc tính from datetime import datetime
import pytz
datetime_in_Lagos = datetime.now[pytz.timezone['Africa/Lagos']]
print[datetime_in_Lagos]
# 2022-09-20 12:53:27.225570+01:00
2 - giống như chúng ta đã làm trong ví dụ trước. Đó làfrom datetime import datetime
import pytz
all_timezones = pytz.all_timezones
print[all_timezones]
# [List of all timezones...]
Tóm lược
Trong bài viết này, chúng ta đã nói về việc lấy ngày và giờ của ngày hôm nay bằng cách sử dụng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 trong PythonChúng ta đã thấy một số ví dụ cho thấy cách sử dụng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 và các thuộc tính của nóCuối cùng, chúng ta đã biết cách lấy ngày và giờ ở các địa điểm cụ thể trên khắp thế giới bằng cách sử dụng hàm
from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
1 và mô-đun from datetime import datetime
current_dateTime = datetime.now[]
print[current_dateTime]
# 2022-09-20 10:27:21.240752
2Mã hóa vui vẻ
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Tiểu sử của tác giả này có thể được tìm thấy trong các bài viết của ông
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu