Nếu bạn muốn một giải pháp chỉ sử dụng thư viện tiêu chuẩn và hoạt động trong cả Python 2 và Python 3, hãy xem câu trả lời của JFS.
Trong Python 3.9+: >>> datetime.datetime.now(datetime.timezone.utc).astimezone()
datetime.datetime(2020, 11, 27, 15, 34, 34, 74823, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))
3 để sử dụng cơ sở dữ liệu múi giờ IANA:
Trong Python 3.9, bạn có thể chỉ định các múi giờ cụ thể bằng thư viện tiêu chuẩn, sử dụng
from datetime import datetime
currentDateAndTime = datetime.now()
print("The current date and time is", currentDateAndTime)
# Output: The current date and time is 2022-03-19 10:05:39.482383
Để có thời gian hiện tại, bạn có thể sử dụng phương thức
Điều này sẽ cung cấp cho bạn thời gian hiện tại ở định dạng 24 giờ:
from datetime import datetime
currentDateAndTime = datetime.now()
print("The current date and time is", currentDateAndTime)
# Output: The current date and time is 2022-03-19 10:05:39.482383
currentTime = currentDateAndTime.strftime("%H:%M:%S")
print("The current time is", currentTime)
# The current time is 10:06:55
Thuộc tính của hàm >>> from zoneinfo import ZoneInfo
>>> datetime.datetime.now(ZoneInfo("America/Los_Angeles"))
datetime.datetime(2020, 11, 27, 6, 34, 34, 74823, tzinfo=zoneinfo.ZoneInfo(key='America/Los_Angeles'))
1
Làm thế nào để có được thời gian hiện tại của một múi giờ với
from datetime import datetime
currentDateAndTime = datetime.now()
print("The current year is ", currentDateAndTime.year) # Output: The current year is 2022
print("The current month is ", currentDateAndTime.month) # Output: The current month is 3
print("The current day is ", currentDateAndTime.day) # Output: The current day is 19
print("The current hour is ", currentDateAndTime.hour) # Output: The current hour is 10
print("The current minute is ", currentDateAndTime.minute) # Output: The current minute is 49
print("The current second is ", currentDateAndTime.second) # Output: The current second is 18
Làm thế nào để có được thời gian hiện tại của một múi giờ với >>> datetime.datetime.now(datetime.timezone.utc).astimezone()
datetime.datetime(2020, 11, 27, 15, 34, 34, 74823, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))
7
from datetime import datetime
import pytz
zones = pytz.all_timezones
print(zones)
# Output: all timezones of the world. Massive!
Điều này sẽ cung cấp cho bạn thời gian hiện tại ở định dạng 24 giờ:
from datetime import datetime
import pytz
newYorkTz = pytz.timezone("America/New_York")
timeInNewYork = datetime.now(newYorkTz)
currentTimeInNewYork = timeInNewYork.strftime("%H:%M:%S")
print("The current time in New York is:", currentTimeInNewYork)
# Output: The current time in New York is: 05:36:59
Điều này sẽ cung cấp cho bạn thời gian hiện tại ở định dạng 24 giờ:
DateTime hiện () sử dụng thời gian nào?
Ngày giờ. UTCNOW là thời gian theo tiêu chuẩn thời gian phổ biến phối hợp và giống nhau trong suốt thời gian. Điều này có nghĩa là DateTime. UTCNow có cùng giá trị trong múi giờ GMT +2 và trong múi giờ GMT -7 và tất cả các mốc thời gian khác.GMT +2 timezone and in the GMT -7 timezone and all other timezones.
DateTime Python sử dụng thời gian nào?
Python thường được sử dụng trên máy chủ.Múi giờ cục bộ trên máy chủ thường là vô nghĩa và phải luôn luôn được đặt thành UTC.Đặt tzinfo DateTime theo cách này thất bại trong một số trường hợp.Sử dụng tốt hơn UTC, sau đó định vị vào múi giờ chỉ muốn trên đầu ra.UTC. Setting datetime tzinfo this way fails in some cases. better use UTC, then localize to the wanted timezone only on output.
Python có phải là DateTime trong UTC không?
Bạn có thể sử dụng mô -đun DateTime để chuyển đổi DateTime thành dấu thời gian UTC trong Python.Nếu bạn đã có đối tượng DateTime trong UTC, bạn có thể dấu thời gian () để có dấu thời gian UTC.Hàm này trả về thời gian kể từ thời đại cho đối tượng DateTime đó.. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object.
DateTime DateTime bây giờ () trong Python là gì?
ngày giờ.Bây giờ () Phương thức chứa các năm, tháng, ngày, giờ, phút, thứ hai và micro giây (được biểu thị bằng yyyy-mm-dd hh: mm: ss. ffffff).Nó cũng chấp nhận một tham số TIME_ZONE tùy chọn, được đặt thành không có mặc định.contains the year, month, day, hour, minute, second, and microsecond (expressed as YYYY-MM-DD hh:mm:ss. ffffff ). It also accepts an optional time_zone parameter, which is set to None by default.