MODULES PART -3

Python datetime:

Python has a module named datetime to work with dates and times. Let's create a few simple programs related to date and time

Example 1: Get Current Date and Time

import datetime

datetime_object = datetime.datetime.now()

print(datetime_object)

When you run the program, the output will be something like:

2018-12-19 09:26:03.478039

Here, we have imported datetime module using import datetime statement.

One of the classes defined in the datetime module is datetime class. We then used now() method to create a datetime object containing the current local date and time.

Example 2: Get Current Date

import datetime

date_object = datetime.date.today()

print(date_object)

When you run the program, the output will be something like:

2018-12-19

In this program, we have used today() method defined in the date class to get a date object containing the current local date.

Commonly used classes in the datetime module are:

date Class

time Class

datetime Class

timedelta Class

datetime.date Class

You can instantiate date objects from the date class. A date object represents a date (year, month and day).

Example 3: Date object to represent a date

import datetime

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

print(d)

When you run the program, the output will be:

2019-04-13

If you are wondering, date() in the above example is a constructor of the date class. The constructor takes three arguments: year, month and day.

The variable a is a date object.

Example 4: Print today's year, month and day

We can get year, month, day, day of the week etc. from the date object easily. Here's how:

from datetime import date

# date object of today's date

today = date.today()

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

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

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

datetime.datetime

The datetime module has a class named dateclass that can contain information from both date and time objects.


Example 5: Python datetime object

 

from datetime import datetime

 

#datetime(year, month, day)

a = datetime(2018, 11, 28)

print(a)

 

# datetime(year, month, day, hour, minute, second, microsecond)

b = datetime(2017, 11, 28, 23, 55, 59, 342380)

print(b)

When you run the program, the output will be:

 

2018-11-28 00:00:00

2017-11-28 23:55:59.342380

 

datetime.timedelta

timedelta object represents the difference between two dates or times.

Example 6: Difference between two dates and times

 
from datetime import datetime, date
 
t1 = date(year = 2018, month = 7, day = 12)
t2 = date(year = 2017, month = 12, day = 23)
t3 = t1 - t2
print("t3 =", t3)
 
t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33)
t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)
t6 = t4 - t5
print("t6 =", t6)
 
print("type of t3 =", type(t3)) 
print("type of t6 =", type(t6))  

When you run the program, the output will be:

 
t3 = 201 days, 0:00:00
t6 = -333 days, 1:14:20
type of t3 = <class 'datetime.timedelta'>
type of t6 = <class 'datetime.timedelta'>

Notice, both t3 and t6 are of <class 'datetime.timedelta'> type.

 

Example 7: Difference between two timedelta objects

 
from datetime import timedelta
 
t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33)
t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54)
t3 = t1 - t2
 
print("t3 =", t3)

When you run the program, the output will be:

 
t3 = 14 days, 13:55:39

Here, we have created two timedelta objects t1 and t2, and their difference is printed on the screen.

 

Python strftime() - datetime object to string

The strftime() method is defined under classes datedatetime and time. The method creates a formatted string from a given datedatetime or time object.

Example 15: Format date using strftime()

 
from datetime import datetime
 
# current date and time
now = datetime.now()
 
t = now.strftime("%H:%M:%S")
print("time:", t)
 
s1 = now.strftime("%m/%d/%Y, %H:%M:%S")
# mm/dd/YY H:M:S format
print("s1:", s1)
 
s2 = now.strftime("%d/%m/%Y, %H:%M:%S")
# dd/mm/YY H:M:S format
print("s2:", s2)

When you run the program, the output will be something like:

 
time: 04:34:52
s1: 12/26/2018, 04:34:52
s2: 26/12/2018, 04:34:52

Here, %Y%m%d%H etc. are format codes. The strftime() method takes one or more format codes and returns a formatted string based on it.

In the above program, ts1 and s2 are strings.

%Y - year [0001,..., 2018, 2019,..., 9999]

%m - month [01, 02, ..., 11, 12]

%d - day [01, 02, ..., 30, 31]

%H - hour [00, 01, ..., 22, 23

%M - minute [00, 01, ..., 58, 59]

%S - second [00, 01, ..., 58, 59]

 

You could learn more examples and deeper classes in datetime module here:

https://www.w3schools.com/python/python_datetime.asp

https://www.programiz.com/python-programming/datetime

 

 

 

 


Comments