Get Current Date in Python - date formats in python
Last updated Jan 18, 2021How 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() |
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
(year, week, day)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 |
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 # Textual month, day and year # mm/dd/y # Month abbreviation, day and year |
Output
todate1 = 18/01/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 :
|
|
|
|
726 Views |