Python có đi kèm với datetime không?

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
5

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, 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ới

Mục lục

  • 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
  • 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
  • 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
  • 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

Đ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ày

from 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
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

Để 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

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

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
3

Bạ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
3

from datetime import datetime
4

Sau đó, 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
5

Trong đoạn mã dưới đây, tôi có thể lấy thời gian ở New York

from datetime import datetime
6

Là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
    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
    
    9 của 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, 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 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
    
    
    0 [dành cho múi giờ của New York]
  • Để có được thời gian hiện tại ở New York, tôi đã 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
    
    5 và chuyển vào đó biến mà tôi đã tạo để lưu múi giờ ở New York
  • Để 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
    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 trên biến
    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
    
    
    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ối

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ạn

Khi đượ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ới

Cả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

Kolade Chris

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

datetime có phải là thư viện Python chuẩn không?

Ngày giờ. Thư viện datetime cung cấp nhiều công cụ để làm việc với ngày và giờ trong Python. Chúng ta có thể dễ dàng lấy thời gian hiện tại, trừ hai ngày hoặc chuyển đổi ngày thành các chuỗi có định dạng tùy chỉnh bằng cách sử dụng mô-đun datetime. Sau khi nhập thư viện, chúng tôi tạo một đối tượng datetime với datetime.

Làm cách nào để thêm ngày giờ trong Python?

Việc bổ sung này có thể được thực hiện bằng cách sử dụng datetime. hàm timedelta[] . Hàm timedelta[] được sử dụng để tính toán sự khác biệt về ngày tháng và cũng có thể được sử dụng để thao tác ngày tháng trong Python. Đây là một trong những cách dễ nhất để thực hiện các thao tác ngày tháng.

Làm cách nào để lấy ngày giờ bằng Python?

Lấy ngày giờ hiện tại bằng Python . Ở đây, chúng tôi đã sử dụng datetime. now[] để lấy ngày giờ hiện tại. Sau đó, chúng tôi đã sử dụng strftime[] để tạo chuỗi biểu thị ngày và giờ ở định dạng khác. use the datetime class of the datetime module. Here, we have used datetime.now[] to get the current date and time. Then, we used strftime[] to create a string representing date and time in another format.

Chủ Đề