Are you interested to learn how to get the current date and time using python? then this tutorial will help you out from the stress. Just follow the modules covered on this page and quickly understand the process of getting the current date and time in python using various methods. Also, you will study to display today’s date and the current date and time in Python apart from the format the date and time in various formats by strftime() method.
In this Python’s Display Date and Time Tutorial, you’ll learn the following stuff:
- Get Current Date Time in Python with datetime Module
- Example: Python get today’s date
- Options for datetime formatting
- Prerequisites
- Display current date and time in Python
- Use strftime() to display Time and Date in Python
- Example: Get the current date and time using now()
Get Current Date Time in Python with datetime Module
Python datetime module supplies classes for handling dates and times in both simple and complex ways. datetime.now(tz=None) returns the current local date and time. If optional argument tz is None or not defined, this is like today().
For creating and accessing a new file then make use of the below command line:
sudo nano python_date.py
After this, it will notify you the file doesn’t exist and demand you to create it. Tap on yes and add the below in your text editor.
Example: Python get today’s date
from datetime import date today = date.today() print("Today's date:", today)
In the above python code, we have imported the date class from the datetime module. Next, we used the date.today()
method to display the current local date.
Also Check: Using Python Display Calendars
- Python Programming – Date and Time Functions
- Using Python to Display Calendars | Learn How to Print a Calendar in Python with Example
- Python Code Snippets: Solving the Quadratic Equation | Python Program to Solve Quadratic Equation
Options for datetime formatting
Python has plenty of options to configure the way date and time are formated. Check out the following code snippet:
import datetime e = datetime.datetime.now() print ("Current date and time = %s" % e) print ("Today's date: = %s/%s/%s" % (e.day, e.month, e.year)) print ("The time is now: = %s:%s:%s" % (e.hour, e.minute, e.second))
Prerequisites
- Python installed
- Command-line / terminal window access
- A user account with root or sudo privileges
- Preferred text editor (in this case nano)
Display current date and time in Python
from datetime import datetime now = datetime.now() print (now.strftime("%Y-%m-%d %H:%M:%S"))
Output:
2020-06-19 10:34:45
Use strftime() to display Time and Date in Python
If you need to get the current time and date using python then the code to execute these actions is very simple by using the strftime() function. In order to start this method, you should import the time module initially and make use of the following few short lines of python code to display the accurate current date and time. Have a glance at the below python code snippet:
import time ## 24 hour format ## print (time.strftime("%H:%M:%S")) ## 12 hour format ## print (time.strftime("%I:%M:%S")) ## dd/mm/yyyy format print (time.strftime("%d/%m/%Y")) ## mm/dd/yyyy format print (time.strftime("%m/%d/%Y")) ## dd/mm/yyyy hh:mm:ss format print (time.strftime('%d/%m/%Y %H:%M:%S')) ## mm/dd/yyyy hh:mm:ss format print (time.strftime('%m/%d/%Y %H:%M:%S'))
In the above illustration, you’ll notice four diverse options for the formats in which you can display your date and time. For a time, there’s the 24-hour format, which refers to a clock that covers all 24 hours, instead of a 12-hour clock that just repeats twice in a day, and there’s also the option for a 12-hour format. There’s an instance that shows how to display your date in the dd/mm/yyyy format, and one that proves how to display the date in mm/dd/yyyy, which is the date format that’s most common in the US.
Ultimately, there’s an illustration that connects both date and time in the day/month/year, hour:min:sec format (and an example with the month coming before the day, for coders in the US). Feel free to add these to any of your Python projects where you want to easily and quickly display the date and time.
Example: Get the current date and time using now()
from datetime import datetime # datetime object containing current date and time now = datetime.now() print("now =", now) # dd/mm/YY H:M:S dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print("date and time =", dt_string)
Result:
now = 2021-06-25 07:58:56.550604 date and time = 25/06/2021 07:58:56