Python program to get current date and time; In this python article, we would love to share with you how to get the current date & time, get yesterday date, and how to get next day/date in python. And you will also learn how to format the date and time in different formats by using python strftime() method.
There are a number of approaches you can take to get the current date & time, next and yesterday’s date. In this python article, we will demonstrate the date class of the DateTime module to perform/execute this task.
Python Program to Get Current Date and Time Example
Here are some programs to get current, next and previous date with times in python:
- 1: Python get the current date
- 2: Current date in different formats
- 3: Get the current date and time
- 4: Python get yesterday date
- 5: how to get next day date in python
1: Python get the current date
from datetime import date
today = date.today()
print("Today's date:", today)
After executing the program, the output will be:
Today's date: 2020-04-26
First of all, we have imported the date
class from the datetime
module. Then, we used the date.today()
method to get the current local date.
2: Current date in different formats
from datetime import date
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)
# Textual month, day and year
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)
# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)
# Month abbreviation, day and year
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
After executing the program, the output will be:
d1 = 16/09/2019 d2 = September 16, 2019 d3 = 09/16/19 d4 = Sep-16-2019
If you need to get both the current date and time in python, you can use datetime
class of the datetime
module.
3: Get the current date and time
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)
After executing the program, the output will be:
now = 2020-04-26 12:29:27.316232 date and time = 26/04/2020 12:29:27
In the above python program, we have imported DateTime module and use datetime.now()
to get the current date and time. Then, we used strftime()
to create a string representing date and time in another format.
4: Python get yesterday date
from datetime import date
from datetime import timedelta
# Get today's date
today = date.today()
print("Today is: ", today)
# Yesterday date
yesterday = today - timedelta(days = 1)
print("Yesterday was: ", yesterday)
After executing the program, the output will be:
Today is: 2020-04-26 Yesterday was: 2020-04-25
In the above python program, we have used datetime.now()
to get the current date and time. Then, Use today – datetime. timedelta(days=1) to subtract one day from the previous result today and print date yesterday date in python.
5: how to get next day date in python
from datetime import date
from datetime import timedelta
# Get today's date
today = date.today()
print("Today is: ", today)
# Yesterday date
yesterday = today + timedelta(days = 1)
print("Next date will be : ", yesterday)
After executing the program, the output will be:
Today is: 2020-04-26 Next date will be : 2020-04-27
In the above python program, we have used datetime.now()
to get the current date and time. Then, Use today + datetime. timedelta(days=1) to add one day from the previous result today and print date next date in python.
Recommended Python Programs
- Python Program to Add Two Numbers
- Python Program to Find/Calculate Sum of n Numbers
- Python Program to Find/Calculate Average of 3, 4, 5…n numbers
- Python Program to Print ASCII Value of Character
- Write a Program to Calculate Simple Interest in Python
- Python Program to Compute Compound Interest
- Leap Year Program in Python
- Python Program to Print Star Pattern
- Number Pattern Programs in Python
- Python Program to Print Even and Odd numbers From 1 to N
- Python Abs() Function: For Absolute Value
- How to Check Whether a Number is Fibonacci or Not in Python
- Python: Program to Find Power of Number
- Python Program to Print Prime Number 1 to N
- How to Find Square of Number in Python
- Python Program to Calculate Cube of Number
- Python Program to Find LCM of Two Numbers
- BMI (Body Mass Index) Calculator in Python
- Palindrome Program in Python using while loop, Function, etc
- Python: Program to Count Total Number of Bits in Number
- Python Random Number Generator Code
- Python Program to Calculate n-th term of a Fibonacci Series
- Zip Zap Zoom Python Program
- Python: program to convert Celsius to Fahrenheit
- Python Program to Swap Two Numbers
- Python Program to Get Standard Deviation
- Python Program to Find the Variance
- Python Program to Convert Height in cm to Feet and Inches
- Python Program to Convert Meters into Yards, Yards into Meters
- Python Program to Convert Kilometers to Meters, Miles
- Python Program to Find Perfect Number
- Python: Program to Find Strong Number
- Python Program Create Basic Calculator
- Python Program For math.floor() Method
- Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
- Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
- Python Program to Find Roots of Quadratic Equation
- Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
- Python Program to Check Given Input is Alphabet, Number or Special Character
- Python Program to Check IF a Number is Power of Another Number
- Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
- Python Program to Print Binary Value of Numbers From 1 to N