0% found this document useful (0 votes)
56 views4 pages

Datetime

The document discusses Python's datetime module which is used to work with dates and times. It provides examples of getting the current date and time, creating date objects, getting individual date parts like year, month and day, converting strings to datetime objects, calculating the difference between dates, using timedeltas to add or subtract time, and getting future dates.

Uploaded by

sekhar s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

Datetime

The document discusses Python's datetime module which is used to work with dates and times. It provides examples of getting the current date and time, creating date objects, getting individual date parts like year, month and day, converting strings to datetime objects, calculating the difference between dates, using timedeltas to add or subtract time, and getting future dates.

Uploaded by

sekhar s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python has a module named 

datetime to work with dates and times

Example 1: Get Current Date and Time


import datetime

datetime = datetime.datetime.now()

print(datetime)

Example 2: Get Current Date

import datetime

date = datetime.date.today()

print(date)

outout:2022-11-02

Commonly used classes in the datetime module are:

 date Class

 time Class

 datetime Class

 timedelta Class

Example 3: create a Date object


import datetime

d = datetime.date(2019, 4, 13)

print(d)

o/p: 2019-04-13
4 Current Day
from datetime import date

today = date.today()

print("Current date =", today)

o/p: Current date = 2022-11-02

5 Day &Month&Year

from datetime import date

today = date.today()

print("Current year:", today.year)

print("Current month:", today.month)

print("Current day:", today.day)

6 Strptime
We can convert a string to a datetime object
from datetime import datetime

date_string = "21 January 2022"

print("date_string =", date_string)

dateobject = datetime.strptime(date_string, "%d %B, %Y")

print("dateobject:", dateobject)
Difference between two dates
from datetime import datetime

# dates in string format

str_d1 = '2018/01/10'

str_d2 = '2022/2/20'

# convert string to date object

d1 = datetime.strptime(str_d1, "%Y/%m/%d")

d2 = datetime.strptime(str_d2, "%Y/%m/%d")

# difference between dates in timedelta

delta = d2 - d1

print(f'Difference is {delta.days} days')

Time Delta:

We can use the timedelta to add or subtract weeks, days, hours, minutes,


seconds, microseconds, and milliseconds from a given date and time.

from datetime import datetime

currentdate = datetime.now()

loanterm = datetime(year=2018, month=1, day=21, hour=12, minute=30)

# Difference between two dates

# Get timedelta

timedelta = current_date - loanterm

print(timedelta)

print(type(timedelta))

Future Date:
from datetime import datetime, timedelta

current_date = datetime.now()

print('Given Date:', current_date)

# add 4 weeks in given date

new_date = current_date + timedelta(weeks=4)

print('Future Date:', new_date)

You might also like