top of page

Python Dates

In python, it is done using the DateTime module.

First, need to import datetime module:

>>> Import datetime

>>> print(datetime.now())

Displaying current Date and Time:

>>>  datetime.date.today()

It displays the current date.

>>>  print(datetime.date(2019, 4, 13))

It displays the date in proper formate.

How to creating a datetime object?

To creating datetime object we use "datetime" class with "datetime" module

Example:

import datetime

x = datetime.datetime(2019, 6, 10)

print(x)

 

Output:

2019-06-10 00:00:00

strftime() method

It used to specify the formate of given datetime.

 

Example:

import datetime

x = datetime.datetime(2019, 6, 10)

print(x.strftime("%B"))

 

Output:

It returns the month:

June

List of other strftime() codes

%A - Use for weekday short format

%a -  Weekday without abbreviation

%w - Weekday in number format(0-6), where 0 is used for Sunday

%d - Use for the day of the month(0-31)

%b - Use for month name with the abbreviation, for example - dec

And more others.

You can read complete datetime using python doc –

https://docs.python.org/3/library/datetime.html

bottom of page