Getting the Current Date and Time

Posted in Python by Dirk - last update: Feb 08, 2024

To get the current date you can use the date.today() function from the datetime module. This function will return the current date in the YYYY-MM-DD format.

To get the current time in Python you can use the datetime.now().time() function from the datetime module. It will return the current time. By default is formatted as "HH:MM:SS.microseconds".

The datetime module

In Python, you can work with dates and times using the datetime module. It provides classes for working with dates and times, along with various functions for formatting and manipulating them.

Getting the Current Date

To get the current date, you can use the date class from the datetime module. Here’s an example:

from datetime import date

# Get the current date
current_date = date.today()

# Print the result
print("Current Date:", current_date)

The date.today() function is a part of the date class in Python’s datetime module. It returns the current local date in the format YYYY-MM-DD. It does not include the time information, providing only the year, month, and day. If you need this information as well you should use the datetime.now() function

Output:

Current Date: 2024-02-08

Getting the Current Time

To get the current time in Python you can use the datetime.now().time() function from the datetime module. It will return the current time. By default is formatted as “HH:MM:SS.microseconds”.

Here is an example:

from datetime import time

# Get the current time
current_time = datetime.now().time()

# Print the result
print("Current Time:", current_time)

Output:

Current Time: 14:42:55.971754

Getting Current Date and Time

To get the current date and time, you can use the datetime class from the datetime module. Here’s an example:

from datetime import datetime

# Get the current date and time
current_datetime = datetime.now()

# Print the result
print("Current Date and Time:", current_datetime)

Output:

Current Date and Time: 2024-02-08 14:45:04.486869

The datetime.now() function returns a datetime object, and this object has several attributes. Some of the common attributes include year, month, day, hour, minute, second, and microsecond. The attributes provide individual components of the date and time, allowing you to work with them separately as needed.

Here’s a more detailed example:

from datetime import datetime

# Get the current date and time
current_datetime = datetime.now()

# Access attributes
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day
hour = current_datetime.hour
minute = current_datetime.minute
second = current_datetime.second
microsecond = current_datetime.microsecond

# Print the attributes
print("Year:", year)
print("Month:", month)
print("Day:", day)
print("Hour:", hour)
print("Minute:", minute)
print("Second:", second)
print("Microsecond:", microsecond)

Output:

Year: 2024
Month: 2
Day: 8
Hour: 13
Minute: 51
Second: 30
Microsecond: 661041

Formatting Dates and Times:

You can format dates and times in various ways using the ‘strftime’ method. Here are some common format codes:

  • %Y: Year with century as a decimal number.
  • %m: Month as a zero-padded decimal number.
  • %d: Day of the month as a zero-padded decimal number.
  • %H: Hour (00-23).
  • %M: Minute.
  • %S: Second.
 
# Format the current date and time
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")

# Print the formatted result
print("Formatted Date and Time:", formatted_datetime)

Output:

Formatted Date and Time: 2024-02-08 14:47:47

Other articles