Python Program to Find the Number of Weeks between two Dates

In the previous article, we have discussed Python Program to Print all Disarium Numbers within Given range
DateTime:

It is a date and time combination with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.

Date and time are not data types in Python, but a module called DateTime can be imported to work with both the date and the time. There is no need to install the Datetime module externally because it is built into Python.

The Datetime module includes classes for working with dates and times. These classes offer a variety of functions for working with dates, times, and time intervals. In Python, date and DateTime are objects, so when you manipulate them, you are actually manipulating objects rather than strings or timestamps.

Given two dates, and the task is to find the number of weeks between the given two dates.

Examples:

Example1:

Input:

Given First date =2003-11- 10  (YY-MM-DD)
Given second date=2007-4- 12

Output:

The number of weeks between two given dates =  178

Example 2:

Input:

Given First date =1998-5-16 (YY-MM-DD)
Given second date=2001-7- 9

Output:

The number of weeks between two given dates =  164

Program to Find the Number of Weeks between two Dates

Below are the ways to Find the Number of Weeks between two Dates.

Method #1: Using DateTime Module (Static input)

Approach:

  • Import DateTime module using date keyword.
  • Give the First date as static input in the format of YY, MM, DD, and store it in a variable.
  • Give the Second date as static input in the format of YY, MM, DD and store it in another variable.
  • Calculate the absolute difference between the above given two dates using abs(date1-date2) and store it in another variable.
  • Divide the above-got number of days by 7, using the floor division operator, and store it in another variable.
  • Print the number of weeks between the two above given dates.
  • The Exit of the program.

Below is the implementation:

# Import datetime module using date keyword.
from datetime import date
# Give the First date as static input in the format of YY,MM,DD and store it in a variable.
fst_dat_1 = date(2003, 11, 10)
# Give the Second date as static input in the format of YY,MM,DD and store it in another variable.
secnd_dat_2 = date(2007, 4, 12)
# Calculate the absolute difference between the above given two dates using
# abs(date1-date2) and store it in another variable.
no_dayss = abs(fst_dat_1-secnd_dat_2).days
# Divide the above got number of days by 7 ,using floor division operator and
# store it in another variable.
no_weks = no_dayss//7
# Print the number of weeks between  two  above given dates.
print(" The number of weeks between two given dates = ", no_weks)

Output:

The number of weeks between two given dates = 178

Method #2: Using DateTime Module (User input)

Approach:

  • Import DateTime module using date keyword.
  • Give the First date as user input in the format of YY, MM, DD as a string using input(), int(), split() functions function and store it in a variable.
  • Give the Second date as user input in the format of YY, MM, DD as a string using input(), int(), split() functions and store it in another variable.
  • Calculate the absolute difference between the above given two dates using abs(date1-date2) and store it in another variable.
  • Divide the above-got number of days by 7, using the floor division operator, and store it in another variable.
  • Print the number of weeks between the two above given dates.
  • The Exit of the program.

Below is the implementation:

# Import datetime module using date keyword.
from datetime import date
# Give the First date as user input in the format of YY, MM, DD
# as a string using input(),int(),split() functions and store it in a variable.
yy1, mm1, dd1 = map(int, input(
    'Enter year month and date separated by spaces = ').split())
fst_dat_1 = date(yy1, mm1, dd1)
# Give the Second date as user  input using input(),int(),split() functions
# in the format of YY,MM,DD and store it in another variable.
yy2, mm2, dd2 = map(int, input(
    'Enter year month and date separated by spaces = ').split())
secnd_dat_2 = date(yy2, mm2, dd2)
# Calculate the absolute difference between the above given two dates using
# abs(date1-date2) and store it in another variable.
no_dayss = abs(fst_dat_1-secnd_dat_2).days
# Divide the above got number of days by 7 ,using floor division operator and
# store it in another variable.
no_weks = no_dayss//7
# Print the number of weeks between  two  above given dates.
print(" The number of weeks between two given dates = ", no_weks)

Output:

Enter year month and date separated by spaces = 2001 02 11
Enter year month and date separated by spaces = 2001 09 28
The number of weeks between two given dates = 32

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.