Python Program to Get Current Date and Time

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

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Print Prime Number 1 to N
  15. How to Find Square of Number in Python
  16. Python Program to Calculate Cube of Number
  17. Python Program to Find LCM of Two Numbers
  18. BMI (Body Mass Index) Calculator in Python
  19. Palindrome Program in Python using while loop, Function, etc
  20. Python: Program to Count Total Number of Bits in Number
  21. Python Random Number Generator Code
  22. Python Program to Calculate n-th term of a Fibonacci Series
  23. Zip Zap Zoom Python Program
  24. Python: program to convert Celsius to Fahrenheit
  25. Python Program to Swap Two Numbers
  26. Python Program to Get Standard Deviation
  27. Python Program to Find the Variance
  28. Python Program to Convert Height in cm to Feet and Inches
  29. Python Program to Convert Meters into Yards, Yards into Meters
  30. Python Program to Convert Kilometers to Meters, Miles
  31. Python Program to Find Perfect Number
  32. Python: Program to Find Strong Number
  33. Python Program Create Basic Calculator
  34. Python Program For math.floor() Method
  35. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  36. Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
  37. Python Program to Find Roots of Quadratic Equation
  38. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  39. Python Program to Check Given Input is Alphabet, Number or Special Character
  40. Python Program to Check IF a Number is Power of Another Number
  41. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  42. Python Program to Print Binary Value of Numbers From 1 to N

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *