-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Python has a built-in module called datetime
to work with dates and times.
To use it, you must import the module:
import datetime
import datetime
now = datetime.datetime.now()
print(now)
today = datetime.date.today()
print(today)
current_time = datetime.datetime.now().time()
print(current_time)
d = datetime.datetime(2023, 5, 17)
print(d)
now = datetime.datetime.now()
print(now.year)
print(now.month)
print(now.day)
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d")) # 2025-06-21
print(now.strftime("%A")) # Friday
print(now.strftime("%I:%M %p")) # 05:30 PM
Common format codes:
Code | Meaning |
---|---|
%Y |
Year |
%m |
Month (01–12) |
%d |
Day |
%H |
Hour (00–23) |
%I |
Hour (01–12) |
%M |
Minute |
%p |
AM or PM |
%A |
Weekday name |
from datetime import timedelta, datetime
today = datetime.now()
future = today + timedelta(days=10)
past = today - timedelta(days=5)
print(future)
print(past)
print(datetime.now().isoformat())
Get today’s date
Format it into DD-MM-YYYY
Add 7 days to current date
Extract time only from current timestamp
Q1. Write a Python program to print the current date and time using datetime.now()
.
Q2. Write a Python program to show only the year from today’s date.
Q3. Write a Python program to format today’s date as DD/MM/YYYY
.
Q4. Write a Python program to print the current time in HH:MM AM/PM
format.
Q5. Write a Python program to create a datetime
object for your birthday.
Q6. Write a Python program to add 15 days to the current date using timedelta
.
Q7. Write a Python program to subtract 7 days from today’s date using timedelta
.
Q8. Write a Python program to show the weekday name for today’s date (e.g., "Monday").
Q9. Write a Python program to print only the time from datetime.now()
.
Q10. Write a Python program to display the current date in ISO format using .isoformat()
.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms