February 13, 2023

Python – Date and Time 2

In this lesson, you will learn how to work with Date, Time and DateTime classes in Python. Also we would learn how to format date and time data.

 

    1. Introduction to Python Date and Time
    2. Working with Current Date and Time
    3. Getting Day, Month, Year and Weekday
    4. Getting both Date and Time
    5. Working with only Time
    6. Formatting using strftime() function
    7. Watch the Video

 

1. Introduction to Python Date and Time

Python provides a number of module and several functions for working with date and time.

Before you can work with date, time and datetime codes, you need to import the necessary modules. These modules are date, time and datetime. This is shown below.

from datetime import date
from datetime import time
from datetime import datetime

 

2. Working with Current Date and Time

The first thing you want to do with date and time is to get today’s date. To do that you use the today() function in the date module.

The code below prints out the today’s date

 

today = date.today()
print("Today's date is ", today)

 

If you run the above code, you will have the output below:

 

Today's date is  2019-02-20

 

3. Getting Day, Month, Year and Weekday

You will notice that the date is made up of day, month and year. We can therefore separate a date in to the three components. This is done using the day, month and year methods.

The code below prints out the day, month and year in today’s date.

 

today = date.today()
print("Today's date is ", today)

print("Day: ", today.day)
print("Month: ", today.month)
print("Year: ", today.year)

 

Run this code and examine the output

You can also get the weekday using the today.weekday() function. If you use this function, you get the weekday’s number. This is such that Monday = 0, Tuesday = 1, Wednesday = 3, and so on. You can try this yourself.

 

4. Getting both Date and Time

The two functions that gives you both date and time together are the today() and the now() functions. You use them to create datetime objects. These function however, are in the datetime module.

The datetime object includes hours, minutes, seconds and microseconds.

The code below prints a datetime object representing the current date and time.

 

today = datetime.today()
print("Today's date is ", today)

 

If you execute this code, the output would be as shown below. You can also get the same output using the datetime.now() function.

 

Today's date is  2019-02-20 03:10:58.165653

 

5. Working with Only Time

To get only the time you need to use the time function. This function takes an argument of a datetime object from the datetime module.

For example, the code below outputs only the current time

# Getting the current time
today = datetime.now()
currenttime = datetime.time(datetime.today())
print("The Current time is: ", currenttime)

 

The output of this code is the current time. Now we would write a code that separates the time into hours, minutes, seconds and microseconds. You can find the code below:

 

# Getting hours, minutes and seconds
today = datetime.now()
currenttime = datetime.time(datetime.today())
print("The Current time is: ", currenttime)

print("Hours ", currenttime.hour)
print("Minutes ", currenttime.minute)
print("Seconds ", currenttime.second)
print("Microseconds", currenttime.microsecond)

 

The output of the code is given below

 

The Current time is:  03:36:17.469424
Hours  3
Minutes  36
Seconds  17
Microseconds 469424

 

6. Formatting Date and Time using strftime()

The strftime() is a method that applies to both date and time. It take various format parameters to format the date/time objects.

For example, the code below displays the current date and time in 24hour format. It also displays the weekday.

 

print(datetime.now().strftime("%A %b %d %Y by %I:%M %p"))

 

Find a complete list of strftime() function formats here.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments