Get Current Date in Python - date formats in python

Last updated Jan 18, 2021


How to get current date in python. How to print it on the console.

In this post we will learn how to print current date in python.

To get current date in python we nee to import "datetime" module

 

Python program to get Current Date

from datetime import date

todate = date.today()
print("Today's date:", todate)

 

In the above program we imported date class from datetime module.

The date object is return a date like year,month and day.

 

date class properties

The date class contains below proerties

  • date.today()
  • date.fromtimestamp(timestamp)
  • date.fromordinal(ordinal)
  • date.fromisoformat(date_string)
  • date.fromisocalendar(yearweekday)
  • date.min
  • date.max
  • date.year
  • date.month
  • date.day

 

The above python code will return current date in the below format

Today's date: 2021-01-18

 

 

Program to get Current date and time

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

 

This will print the current date and time

now = 2021-01-18 12:06:03.531239

 

Format date and time in Python

To format the Date and time we will use the  strftime() method

 

Python Program to Print Current date in different format

 

from datetime import date

todate = date.today()

# dd/mm/YY
todate1 = todate.strftime("%d/%m/%Y")
print("todate1 =", todate1)

# Textual month, day and year    
todate2 = todate.strftime("%B %d, %Y")
print("todate2 =", todate2)

# mm/dd/y
todate3 = todate.strftime("%m/%d/%y")
print("todate3 =", todate3)

# Month abbreviation, day and year    
todate4 = todate.strftime("%b-%d-%Y")
print("todate4 =", todate4)

 

Output

todate1 = 18/01/2021
todate2 = January 18, 2021
todate3 = 01/18/21
todate4 = Jan-18-2021

 

Other Python Date and Time types

timedelta : This timedelta object will handle the duration and get the difference between different dates 

 

time : This object will return current time of the day

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

186 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions