Date & Time

import datetime

x = datetime.datetime.now()
print(x)
2019-09-26 00:21:06.668559

Date Output

import datetime

x = datetime.datetime.now()

print(x.year)
print(x.strftime("%A"))
2019
Thursday

Creating Date Objects

import datetime

x = datetime.datetime(2020, 5, 17)
print(x)
2020-05-17 00:00:00

The strftime() Method

import datetime

x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
June

timedelta

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļŦāļēāļœāļĨāļ•āđˆāļēāļ‡āļ‚āļ­āļ‡āļ§āļąāļ™āļ—āļĩāđˆ

import datetime
import pytz

my_birthday = datetime.datetime(1985, 10, 20, 17, 55)
brothers_birthday = datetime.datetime(1992, 6, 25, 18, 30)

indy = pytz.timezone("America/Indianapolis")
my_birthday = indy.localize(my_birthday)
brothers_birthday = indy.localize(brothers_birthday)

diff = brothers_birthday - my_birthday
print(diff)
2440 days, 0:35:00

āļŦāļēāļ§āļąāļ™ āđ‚āļ”āļĒāļšāļ§āļāļˆāļēāļāļ§āļąāļ™āļ›āļąāļˆāļˆāļļāļšāļąāļ™āđ„āļ›āļ­āļĩāļ 90 āļ§āļąāļ™

import datetime

today = datetime.datetime.now()
ninety_days = datetime.timedelta(days=90)

target_date = today + ninety_days

target_date.strftime("%A")
'Wednesday'

Last updated