🟡 Advanced Python  ·  Lesson 24

Date and Time Handling

Date और Time Handling

What is Date and Time?

Date and Time means the datetime module helps work with dates, times, durations and formatting.

In real programs, this topic helps in attendance dates. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaAdvanced Python
Professional concepts used to make code reusable, clean and project-ready.
Main Useattendance dates
Example Filedate-time.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for attendance dates.
  • It connects with fee due dates.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
datetimeModule for working with dates and times.
datedate is an important term in this topic.
timedeltaRepresents duration or difference between dates.
strftimeMethod used to format date/time as text.
parsingparsing is an important term in this topic.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
from datetime import datetime, timedelta
now = datetime.now()
exam_date = now + timedelta(days=10)
print("Today:", now.strftime("%d-%m-%Y"))
print("Exam Date:", exam_date.strftime("%d-%m-%Y"))

Complete Example Program

Python – date-time.py
from datetime import datetime, timedelta

now = datetime.now()
exam_date = now + timedelta(days=10)

print("Today:", now.strftime("%d-%m-%Y"))
print("Exam Date:", exam_date.strftime("%d-%m-%Y"))

Expected Output

Today: DD-MM-YYYY Exam Date: DD-MM-YYYY

Program Explanation

  • from datetime import datetime, timedelta imports ready-made features from a module/library.
  • now = datetime.now() stores a value in now.
  • exam_date = now + timedelta(days=10) stores a value in exam_date.
  • print("Today:", now.strftime("%d-%m-%Y")) displays information or calculated result on the screen.
  • print("Exam Date:", exam_date.strftime("%d-%m-%Y")) displays information or calculated result on the screen.

Where will you use it?

  • Attendance dates.
  • Fee due dates.
  • Age and duration calculation.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Type the program in date-time.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to attendance dates.
  4. Write 5 lines explaining the logic in your own words.

Summary

Date and Time is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

Date और Time क्या है?

Date और Time ka matlab hai: The datetime module helps work with dates, times, durations and formatting. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki attendance dates jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye attendance dates me kaam aata hai.
  • Ye fee due dates se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
datetimeModule for working with dates and times.
datedate is an important term in this topic.
timedeltaRepresents duration or difference between dates.
strftimeMethod used to format date/time as text.
parsingparsing is an important term in this topic.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
from datetime import datetime, timedelta
now = datetime.now()
exam_date = now + timedelta(days=10)
print("Today:", now.strftime("%d-%m-%Y"))
print("Exam Date:", exam_date.strftime("%d-%m-%Y"))

Complete Example Program

Python – date-time.py
from datetime import datetime, timedelta

now = datetime.now()
exam_date = now + timedelta(days=10)

print("Today:", now.strftime("%d-%m-%Y"))
print("Exam Date:", exam_date.strftime("%d-%m-%Y"))

Expected Output

Today: DD-MM-YYYY Exam Date: DD-MM-YYYY

Program Explanation

  • from datetime import datetime, timedelta imports ready-made features from a module/library.
  • now = datetime.now() stores a value in now.
  • exam_date = now + timedelta(days=10) stores a value in exam_date.
  • print("Today:", now.strftime("%d-%m-%Y")) displays information or calculated result on the screen.
  • print("Exam Date:", exam_date.strftime("%d-%m-%Y")) displays information or calculated result on the screen.

Practical Uses

  • Attendance dates.
  • Fee due dates.
  • Age and duration calculation.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Program ko date-time.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. attendance dates par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

Date and Time ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial