In this Python tutorial, we will discuss how to get current date and time in Python, and also we will discuss how I get the current date in dd mm yyyy format in Python. Along with that, we are going to cover the following given topics.
- Python program for current date and time
- Python program for displaying current date and time
- Python time get the current year
- Python program to get the current time
Python program for current date and time
- This section will discuss how to get the current date and time in Python.
- The current system date and time may frequently be required by a user for various actions. The many Python built-in functions and modules make it simple to fix this issue.
- By importing the built-in Python package datetime, we can deal with Date methods. To work with dates, we have data objects. Dates in the forms of year, month, day, hour, minute, second, and microsecond are included in this datetime module.
- There are numerous ways to get data about a date object from the datetime module. To calculate the function, a date, month, and year must be provided.
Example:
Let’s take an example and check our first method to get the current date and time in Python.
Method-1: Using the Datetime module
Date and time manipulation classes are available in the datetime module in Python. There may be a variety of applications that need processing times and dates. When working with various dates and time formats, the datetime module comes in quite a way.
To determine the current time and date, use the now() method. This item comes under the datetime module. It gives back the date and time in the format yyy-mm-dd hh:mm:ss: ms.
Example:
import datetime
recent_time_date = datetime.datetime.now()
# Display the value
print("Current time and date:", recent_time_date)
In the following given code, first we imported the datetime module and then used the datetime.now() function. Next we used the now() function to get the current time.
Here is the Screenshot of the following given code
This is how to get the current date and time by using datetime.now() method in Python.
Read: Check if a String contains a Substring in Python
Method 2: By using the pendulum module
Pendulum is a timezone library that makes manipulating dates and times easier in Python. The pendulum module, like the datetime module, provides a now() method that returns the current time and date.
Example:
import pendulum
new_time = pendulum.now()
print(new_time)
Here is the implementation of the following given code
As you can see in the screenshot we have understood how to display the current date and time by using the pendulum module in python.
Method-3: Attributes of Datetime using Datetime
As with time-related attributes like year, month, date, hour, minute, and second, timedate.now() has a variety of features.
Example:
import datetime
new_current_date_time = datetime.datetime.now()
print("The attributes of datetime.now() are :")
print(" Current Year :", new_current_date_time.year)
print("Current Month : ", new_current_date_time.month)
print("Current Day : ", new_current_date_time.day)
print("Current Hour : ", new_current_date_time.hour)
print("Current Minute : ", new_current_date_time.minute)
print("Current Second :", new_current_date_time.second)
print("Current Microsecond :", new_current_date_time.microsecond)
You can refer to the below Screenshot
Method 4: By using the pytz module
The zone affects the values of the date and time. For Python applications that need to interact with users from other countries, these variables must be changed.
The zone indicates that the Python dateTime module is unable to transform the date and time data. Using Python’s pytz module, this issue can be resolved.
Pendulum, on the other hand, is a related library that derives from the datetime module. It is faster than its rival because it takes a approach (pytz).
Example:
import pytz
from datetime import datetime
new_utc = pytz.timezone('UTC')
new_pst = pytz.timezone('America/Los_Angeles')
new_ist = pytz.timezone('Asia/Calcutta')
print('Current Date and Time in UTC =', datetime.now(new_utc))
print('Current Date and Time in PST =', datetime.now(new_pst))
print('Current Date and Time in IST =', datetime.now(new_ist))
Here is the screenshot of the following given code
This is how we can get the current date and time by using the pytz module.
Read Python concatenate list with examples
Python program for displaying current date and time
- Here we will discuss how to display the current date and time in Python.
- The current system date and time may frequently be required by a user for various actions. The many Python built-in functions and modules make it simple to fix this issue.
- To perform this particular task we are going to use the built-in Python package DateTime, we can deal with Date methods. To work with dates, we have data objects. Dates in the forms of year, month, day, hour, minute, second, and microsecond are included in this DateTime module.
Example:
Let’s take an example and check how to display the current date and time in Python.
Source Code:
import datetime
new_time_date = datetime.datetime.now()
# Display the Current-time date
print("Current time and date:", new_time_date)
Here is the Output of the following given code
In this example, we have understood how to display the current date and time in Python.
Read Python binary tree implementation
Python time get the current year
- To get the current year in Python there are various methods to perform this task.
- To find the current year in Python is to use the strfttime() function. The argument for the strftime() function is a string that specifies the date format. The given date is returned as a string in the specified format. For strftime() to return the year of the date object, we will pass it “% Y”.
Example:
import datetime
new_year = datetime.date.today()
result = new_year.strftime("%Y")
print(result)
Here is the execution of the following given code
Method 2: By using the year attribute
The quickest method to find the current year is to use the datetime module. The returned object’s year attribute is directly accessible to us.
Python requires that we import the date class from the datetime module before invoking today().year to retrieve the current year.
Example:
import datetime
new_year = datetime.date.today()
result = new_year.year
print(result)
Here is the Screenshot of the following given code
In this example, we have understood how to get the current year by using attribute year in Python.
Python program to get the current time
- To obtain the current time, use the now() function on a Datetime object. This gives you a detailed time and date report for your present location. Once you have this, you may format the value with the strftime() method to only retrieve the time data.
- In the program mentioned above, we use datetime.now() to retrieve the current date and time. The current date and time were then extracted from a datetime object using the now() method.
Example:
from datetime import datetime
new_time = datetime.now()
result = new_time.strftime("%H:%M:%S")
print("Current Time is :", result)
Here is the execution of the following given code
Method 2: By using the time module
The time module’s localtime() function can be used to determine the current time. Let’s use Python’s localtime() function.
Example:
import time
new_current_time = time.localtime()
result = time.strftime("%H:%M:%S",new_current_time )
print("Current Time is :", result)
You can refer to the below Screenshot
This is how to get the current time by using the time module in Python.
In this tutorial, we have discussed how to get the current date and time in Python, and also we have discussed how I get the current date in dd mm yyyy format in Python. Along with that, we have covered the following given topics.
- Python program for current date and time
- Python program for displaying current date and time
- Python time get the current year
- Python program to get the current time
You may also like the following Python tutorials:
- How to find perfect number in Python
- How to trim a string in Python
- How to reverse a number in Python
- Python Program for even or odd
- Complex Numbers in Python
- Python Palindrome Program
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.