Python program to get the individual components of the current date-time


In Python, we have some time in-built functions i.e. datetime(), today(), now(), and strftime() that can be used to get the individual components of the current date-time. The individual component of the current date-time means the exact present timing and date. To solve this type of problem we will use some predefined method related to the module datetime. For example- The current date and time are important for any work culture.

Syntax

The following syntax is used in the examples are −

datetime.now()

This is a predefined method used in Python that includes years, months, days, hours, minutes, and, seconds.

strftime("%Y-%m-%d %H:%M:%S")

This method is used to represent the data and time by importing the module datetime.

today()

This is a predefined method used in Python that returns the present local date. This method is used when the module named “from datetime import date” is defined.

now()

The now() is an in-built function in Python that is primarily used to find the current date and time.

Example 1

In the following example, we will start the program by importing a module named datetime. Then take the necessary variable ‘now’ and used the predefined ‘datetime.datetime.now()’ method to find the local date and time. Next, print the individual component with now variables such as now.year, now.month, now.day, now.hour, now.minute, and, now.second to get the result.

import datetime
# Obtain the current date and time
now = datetime.datetime.now()
# Print individual components of current date and time 
print("The individual components of the current date and time are:")
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)

Output

The individual components of the current date and time are:
Year: 2023
Month: 5
Day: 30
Hour: 21
Minute: 31
Second: 26

Example 2

In the following example, We will start the program by importing all the content of datetime from the module named datetime which will find the current time. Next, it will import the date from the datetime module that will find the current date. Then initialize the variable named tm to calculate the current time by using a predefined method of datetime.now(). Moving ahead to find the time format by using the strftime() method as an object in tm to store it in the variable current_time. Finally, we are printing the individual date and time separately to get the result.

from datetime import datetime
from datetime import date
# Find the current date and time
tm = datetime.now()
# Create the time format in 12-hour format
# %I for 12-hour format, %p for AM/PM
current_time = tm.strftime("%I:%M:%S %p")  
# Print individual components
print("The individual components of the current date are:")
print("Year:", tm.year)
print("Month:", tm.month)
print("Day:", tm.day)
print("The individual components of the current time are:")
print("Hour:", tm.strftime("%I"))
print("Minute:", tm.strftime("%M"))
print("Second:", tm.strftime("%S"))
print("AM/PM:", tm.strftime("%p"))

Output

The individual components of the current date are:
Year: 2023
Month: 5
Day: 30
The individual components of the current time are:
Hour: 09
Minute: 42
Second: 27
AM/PM: PM

Example 3

Installation Requirement

pip install arrow

The above command is required to install to run the program of this example.

In the following example, start importing the module named arrow that will help to provide its built-in function. Then use the built-in function now() with an arrow module in the variable date. Next, print the date and time format individually and get the result.

import arrow
# Get the current date and time
date = arrow.now()
# Print individual components
print("The individual components of the current date:")
print("Year:", date.year)
print("Month:", date.month)
print("Day:", date.day)
print("The individual components of the current time:")
print("Hour:", date.hour)
print("Minute:", date.minute)
print("Second:", date.second)

Output

The individual components of the current date:
Year: 2023
Month: 5
Day: 30
The individual components of the current time:
Hour: 16
Minute: 19
Second: 5

Conclusion

The above two outputs show the result of the current date and time by using some predefined method related to the module named datetime. For formatting the time format it used the strftime() method. This method also returned the format of a date. There is a module called arrow that also helps to set the individual components of current date-time.

Updated on: 17-Jul-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements