Convert time zones

Convert time zones

EasyDatetime Programs · 15 of 20

Write a Python program to convert 12:00 UTC to Indian Standard Time.

Output
2026-11-02 17:30 IST

Approach

  1. A fixed timezone(timedelta(hours=5, minutes=30)).

Solution

Python
from datetime import datetime, timedelta, timezone utc = datetime(2026, 11, 2, 12, 0, tzinfo=timezone.utc)ist = timezone(timedelta(hours=5, minutes=30), "IST")print(utc.astimezone(ist).strftime("%Y-%m-%d %H:%M %Z"))
Output
2026-11-02 17:30 IST

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

All dates in a range

All dates in a range

EasyDatetime Programs · 13 of 20

Write a Python program to list every date in a week.

Output
2026-11-02 Mon
2026-11-03 Tue
2026-11-04 Wed
2026-11-05 Thu
2026-11-06 Fri
2026-11-07 Sat
2026-11-08 Sun

Approach

  1. Add 0 to 6 days to the start.

Solution

Python
from datetime import date, timedelta start = date(2026, 11, 2)for i in range(7):    d = start + timedelta(days=i)    print(d, d.strftime("%a"))
Output
2026-11-02 Mon
2026-11-03 Tue
2026-11-04 Wed
2026-11-05 Thu
2026-11-06 Fri
2026-11-07 Sat
2026-11-08 Sun

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Difference between two times

Difference between two times

EasyDatetime Programs · 12 of 20

Write a Python program to find hours and minutes between two datetimes.

Input
09:15 to 17:40
Output
8h 25m

Approach

  1. Subtract and split the seconds.

Solution

Python
from datetime import datetime start = datetime(2026, 11, 2, 9, 15)end = datetime(2026, 11, 2, 17, 40)minutes = int((end - start).total_seconds() // 60)print(f"{minutes // 60}h {minutes % 60}m")
Output
8h 25m

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Add months to a date

Add months to a date

MediumDatetime Programs · 10 of 20

Write a Python program to add months to a date, clamping to the month’s last day.

Input
2026-01-31 + 1 month
Output
2026-02-28 2027-02-15

Approach

  1. Calculate the new year and month, then limit the day.

Solution

Python
import calendarfrom datetime import date def add_months(d, months):    month_index = d.month - 1 + months    year = d.year + month_index // 12    month = month_index % 12 + 1    day = min(d.day, calendar.monthrange(year, month)[1])    return date(year, month, day) print(add_months(date(2026, 1, 31), 1), add_months(date(2026, 11, 15), 3))
Output
2026-02-28 2027-02-15

Line by line

Lines 5-7

Work in zero-based months so // and % handle the year change.

Line 8

31 January plus one month becomes 28 February, because February has no 31st.

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Count business days

Count business days

MediumDatetime Programs · 11 of 20

Write a Python program to count the working days (Monday to Friday) between two dates.

Input
2026-11-02 to 2026-11-30
Output
21

Approach

  1. Loop over each day and count weekdays.

Solution

Python
from datetime import date, timedelta def business_days(start, end):    count = 0    d = start    while d <= end:        if d.weekday() < 5:            count += 1        d += timedelta(days=1)    return count print(business_days(date(2026, 11, 2), date(2026, 11, 30)))
Output
21

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Last day of a month

Last day of a month

EasyDatetime Programs · 9 of 20

Write a Python program to find the last day of a month.

Input
February 2028, November 2026
Output
2028-02-29
2026-11-30

Approach

  1. calendar.monthrange returns the number of days.

Solution

Python
import calendarfrom datetime import date for year, month in [(2028, 2), (2026, 11)]:    days = calendar.monthrange(year, month)[1]    print(date(year, month, days))
Output
2028-02-29
2026-11-30

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Is a date a weekend?

Is a date a weekend?

EasyDatetime Programs · 8 of 20

Write a Python program to check whether dates fall on a weekend.

Output
2026-10-31 weekend
2026-11-02 weekday

Approach

  1. weekday() is 5 or 6 for Saturday and Sunday.

Solution

Python
from datetime import date for d in [date(2026, 10, 31), date(2026, 11, 2)]:    print(d, "weekend" if d.weekday() >= 5 else "weekday")
Output
2026-10-31 weekend
2026-11-02 weekday

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Calculate age

Calculate age

EasyDatetime Programs · 6 of 20

Write a Python program to calculate a person’s age on a given date.

Input
born 1995-12-20, on 2026-11-02
Output
30

Approach

  1. Subtract years, then subtract 1 if the birthday has not come yet.

Solution

Python
from datetime import date def age(birth, on):    years = on.year - birth.year    if (on.month, on.day) < (birth.month, birth.day):        years -= 1    return years print(age(date(1995, 12, 20), date(2026, 11, 2)))
Output
30

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Day of the week for a date

Day of the week for a date

EasyDatetime Programs · 7 of 20

Write a Python program to find the weekday of a date.

Input
2026-01-26
Output
0 1 Monday

Approach

  1. weekday() (Monday = 0) or %A.

Solution

Python
from datetime import date d = date(2026, 1, 26)print(d.weekday(), d.isoweekday(), d.strftime("%A"))
Output
0 1 Monday

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Days between two dates

Days between two dates

EasyDatetime Programs · 5 of 20

Write a Python program to count the days between two dates.

Input
2026-01-26 and 2026-08-15
Output
201

Approach

  1. Subtract dates; read .days.

Solution

Python
from datetime import date print((date(2026, 8, 15) - date(2026, 1, 26)).days)
Output
201

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs