Trong các ứng dụng Python của bạn, bạn có thể muốn làm việc với thời gian để thêm các chức năng như dấu thời gian, kiểm tra thời gian hoạt động của người dùng, v.v.
Một trong những mô-đun giúp bạn làm việc với ngày và giờ trong Python là
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
5Với mô-đun
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
5, bạn có thể lấy ngày và giờ hiện tại hoặc ngày và giờ hiện tại trong một múi giờ cụ thểTrong bài viết này, tôi sẽ hướng dẫn các bạn cách lấy thời gian hiện tại trong Python với module
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
5. Tôi cũng sẽ chỉ cho bạn cách lấy thời gian hiện tại ở bất kỳ múi giờ nào trên thế giớiMục lục
- Cách lấy thời gian hiện tại với Mô-đun
5from 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ác thuộc tính của hàm
1from 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ách lấy thời gian hiện tại của múi giờ với
5from 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
- Phần kết luận
Cách lấy thời gian hiện tại với Mô-đun 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
5
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
Điều đầu tiên bạn phải làm là nhập mô-đun
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
5 như thế nàyfrom datetime import datetime
Điều tiếp theo bạn có thể làm để nhanh chóng có được ngày và giờ hiện tại là sử dụng hàm
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
1 từ mô-đun 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
5from 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ó được thời gian hiện tại cụ thể, bạn có thể sử dụng phương thức
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
7 và chuyển vào đó chuỗi 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
8 đại diện cho giờ, phút và giâyĐ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
Các thuộc tính của hàm 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
1
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
Hàm
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
0 có một số thuộc tính mà bạn có thể nhận được năm, tháng, tuần, ngày, giờ, phút và giây của ngày hiện tạiĐoạn mã dưới đây in tất cả các giá trị của các thuộc tính vào thiết bị đầu cuố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
Cách lấy thời gian hiện tại của múi giờ với 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
5
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
Bạn có thể lấy thời gian hiện tại trong một múi giờ cụ thể bằng cách sử dụng mô-đun
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
5 với một mô-đun khác có tên là 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
3Bạn có thể cài đặt mô-đun
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
3 từ pip như thế này. ______65
Điều đầu tiên bạn phải làm là nhập cả hai mô-đun
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
5 và 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
3from datetime import datetime
4Sau đó, bạn có thể kiểm tra tất cả các múi giờ có sẵn bằng đoạn mã bên dưới
from datetime import datetime
5Trong đoạn mã dưới đây, tôi có thể lấy thời gian ở New York
from datetime import datetime
6Làm thế nào tôi có thể có được thời gian hiện tại ở New York?
- Tôi đã đưa vào phương thức
9 của mô-đunfrom 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
3, chuyển vào đó múi giờ chính xác của New York dưới dạng một chuỗi và gán nó cho một biến có tên là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
0 [dành cho múi giờ của New York]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
- Để có được thời gian hiện tại ở New York, tôi đã sử dụng hàm
1 từ mô-đunfrom 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
5 và chuyển vào đó biến mà tôi đã tạo để lưu múi giờ ở New Yorkfrom 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
- Để cuối cùng có được thời gian hiện tại ở New York ở định dạng 24 giờ, tôi đã sử dụng phương pháp
7 trên biếnfrom 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
4 và lưu trữ nó trong một biến có tên là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
5, vì vậy tôi có thể in nó ra thiết bị đầu cuốifrom 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
Phần kết luận
Như đã trình bày trong bài viết này, mô-đun
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
5 rất thuận tiện khi làm việc với thời gian và ngày tháng, và sau đó nhận được thời gian hiện tại ở địa phương của bạnKhi được kết hợp với mô-đun
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
3 mà bạn có thể cài đặt từ pip, bạn cũng có thể sử dụng nó để lấy thời gian hiện tại ở bất kỳ múi giờ nào trên thế giớiCảm ơn bạn đã đọc. Nếu bạn thấy bài viết này hữu ích, bạn có thể chia sẻ nó với bạn bè và gia đình của bạn
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dù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