KwickAcademy Python · 8 min · free
Working with Dates and Times: datetime and time
Learn dates and times in Python: get the current date and IST time, create and format dates with strftime and strptime, use timedelta, and time code.
On screen in this lesson
The tools we will use
| Name | Module | Holds |
|---|---|---|
| date | datetime | year, month, day |
| datetime | datetime | date and clock time |
| timedelta | datetime | a gap, like 14 days |
| sleep, perf_counter | time | pause, stopwatch |
Format codes for strftime
| Code | Means | For 15 Aug 2026 |
|---|---|---|
| %d | day, 2 digits | 15 |
| %m | month, 2 digits | 08 |
| %Y | year, 4 digits | 2026 |
| %B | month name | August |
| %A | weekday name | Saturday |
| %H:%M | 24-hour time | 07:30 |
Quick recap
| date.today() and datetime.now() give the current moment |
| date(2026, 8, 15) makes a fixed date |
| strftime: date to text; strptime: text to date |
| Date minus date gives a timedelta; add one to move a date |
| time.sleep() pauses; perf_counter() times code |
Quick answers
What does subtracting two dates give?
A timedelta, which is a gap in time; .days gives the number of days.
What is the difference between strftime and strptime?
strftime turns a date into text; strptime reads text and makes a datetime.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
How do you get today's date in Python?40 sec
How do you show a date as 15/08/2026?42 sec
What does date minus date give?39 sec
What does time.sleep(1) do?40 secThe full lesson, in text
Hello students, welcome to Kwickprep. How many days are left for your exam? When is your library book due? Python can answer these in one line. Today we will get the current date and time, and create and format dates. Then we will find gaps with timedelta, and use the time module to pause and time code.
A module is a ready-made file of Python code that we bring in with import. Date, from the datetime module, holds a year, a month and a day. Datetime, from the same module, holds a date plus a clock time. Timedelta holds a gap between two moments, like fourteen days. The time module gives sleep, to pause, and perf counter, which works like a stopwatch.
Date dot today gives today's date, and datetime dot now gives the date with the current time. Their answers keep changing, so here we print things that never change. The first is a date object, and the second is a datetime object. Printed as text, a date is always ten characters long, like twenty twenty six, dash, zero nine, dash, seventeen. That is year, then month, then day. Try print of now yourself, and you will see your own clock time.
Datetime dot now uses your computer's clock, and a server abroad may be in another time zone. To be sure of Indian time, pass a time zone. The zoneinfo module knows every zone, and India's is called Asia slash Kolkata. Utc offset prints five hours thirty minutes, because Indian Standard Time is that far ahead of world time, called U T C. Tz name prints the short name, IST. On Windows, you may first need to install the tzdata package with pip.
To make a fixed date, give the year, the month and the day, in that order. Here, the variable d is Independence Day, the fifteenth of August, 2026. Dot year, dot month and dot day take out each part as a number. For a datetime, add the hour and the minute too. Seven thirty in the morning prints with seconds as zero zero.
Pause and predict. Does the twenty ninth of February exist in every year? No, only in a leap year. The loop tries the year 2028, and then 2026. 2028 is a leap year, so the date prints. 2026 is not, so Python raises a value error. The try and except lines catch that error, and print a friendly message instead.
To show a date your way, we use format codes, each starting with a percent sign. Percent small d gives the day in two digits. Percent small m gives the month in two digits. Percent capital Y gives the full four digit year. Percent capital B gives the month name. Percent capital A gives the weekday name. And percent capital H and percent capital M give hours and minutes.
The strftime method, short for string format time, turns a date into text. The first pattern gives the Indian style, day, slash, month, slash, year. The second pattern gives the weekday and the month name. So we learn that Independence Day in 2026 falls on a Saturday. Only the codes change; everything else in the pattern is printed as it is.
Strptime, short for string parse time, does the opposite. It reads text and makes a datetime. The pattern must match the text exactly, with dashes in the same places. Here we read Republic Day, 2027. Dot date keeps only the date part. And the weekday comes out as Tuesday. If the text does not match the pattern, you get a value error.
When you subtract one date from another, Python gives a timedelta, which means a gap in time. Suppose your exam is on the first of March, 2027. We fix today as the seventeenth of September, 2026, so the answer never changes. The gap prints as one hundred sixty five days, with zero hours. Dot days gives just the number, one hundred sixty five.
We can also add a timedelta to a date. A library book is issued on the twenty fifth of September, and it must come back in fourteen days. Timedelta, with days equal to fourteen, makes that gap. Python moves into October correctly by itself. The book is due on the ninth of October, a Friday.
A timedelta works with clock times too. An overnight train departs at ten fifteen at night, and arrives at six forty five the next morning. The variable dep is departure and arr is arrival. Their gap prints as eight hours and thirty minutes. Total seconds gives the gap in seconds, and dividing by three thousand six hundred gives eight point five hours.
Now the time module. Time dot sleep pauses the program for the number of seconds you give. Here is a race countdown. The loop prints three, waits one second, prints two, waits, then prints one. Finally it prints Go. The output looks the same, but it appears slowly, one second apart.
To time code, use perf counter, which works like a stopwatch. Read it before the work, read it again after, and subtract. Here we sleep for half a second, and then add up a million numbers. So the time taken is at least half a second, and it prints True. Print taken itself, and you will see a number like 0.53, which differs on every computer.
Let us revise what we learned today. Date dot today and datetime dot now give the current moment, and a time zone makes it Indian time. Date with a year, month and day makes a fixed date. Strftime turns a date into text, and strptime turns text into a date. One date minus another gives a timedelta, and adding a timedelta moves a date. Time dot sleep pauses, and perf counter times your code.
Courses that teach this
| Course | Unit |
|---|---|
| Programming All levels Python | Modules and Standard Library |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

