Python程序以找到两个给定日期之间的天数

Python程序以找到两个给定日期之间的天数

在本教程中,我们将讨论如何编写一个Python程序来找到两个给定日期之间的天数。

假设我们给出了两个日期,我们期望的输出是:

示例:

Input: Date_1 = 12/10/2021, Date_2 = 31/08/2022
Output: Number of Days between the given Dates are: 323 days
Input: Date_1 = 10/09/2023, Date_2 = 04/02/2025
Output: Number of Days between the given Dates are: 323 days: 513 days

方法1:原生方法

在这种方法中,朴素的解决方案将从日期_1开始,并且将计算直到达到日期_2的天数。这个解决方案将需要超过 O(1) 次。这是一种简单的解决方案,用于计算日期_1之前的总天数,这意味着它将计算从00/00/0000到日期_1的总天数,然后计算日期_2之前的总天数。最后,它将以给定日期之间的总天数的形式返回两个计数之间的差异。

示例:

# First, we will create a class for dates
class date_n:
    def __init__(self, day, month, year):
        self.day = day
        self.month = month
        self.year = year


# For storng number of days in all months from
# January to December.
month_Days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# This function will count the number of leap years from 00/00/0000 to the #given date


def count_Leap_Years(day):

    years = day.year

    # Now, it will check if the current year should be considered for the count          # of leap years or not.
    if (day.month <= 2):
        years -= 1

    # The condition for an year is a leap year: if te year is a multiple of 4, and a            # multiple of 400 but not a multiple of 100.
    return int(years / 4) - int(years / 100) + int(years / 400)


# This function will return number of days between two given dates
def get_difference(date_1, date_2):

    # Now, it will count total number of days before first date "date_1"

    # Then, it will initialize the count by using years and day
    n_1 = date_1.year * 365 + date_1.day

    # then, it will add days for months in the given date
    for K in range(0, date_1.month - 1):
        n_1 += month_Days[K]

    # As every leap year is of 366 days, we will add 
    # a day for every leap year
    n_1 += count_Leap_Years(date_1)

    # SIMILARLY, it will count total number of days before second date "date_2"

    n_2 = date_2.year * 365 + date_2.day
    for K in range(0, date_2.month - 1):
        n_2 += month_Days[K]
    n_2 += count_Leap_Years(date_2)

    # Then, it will return the difference between two counts
    return (n_2 - n_1)


# Driver program
date_1 = date_n(12, 10, 2021)
date_2 = date_n(30, 8, 2022)

print ("Number of Days between the given Dates are: ", get_difference(date_1, date_2), "days")

输出:

Number of Days between the given Dates are:  322 days

方法2:使用Python的datetime模块

在此方法中,我们将看到如何使用Python的内置函数“datetime”来帮助用户解决各种与日期和时间相关的问题。要找到两个日期之间的差异,我们可以以日期类型的格式输入两个日期并相减,这将得到两个给定日期之间的天数输出。

示例:

from datetime import date as date_n

def number_of_days(date_1, date_2):
    return (date_2 - date_1).days

# Driver program
date_1 = date_n(2023, 9, 10)
date_2 = date_n(2025, 2, 4)
print ("Number of Days between the given Dates are: ", number_of_days(date_1, date_2), "days")

输出:

Number of Days between the given Dates are:  513 days

结论

在本教程中,我们讨论了两种不同的方法,用于编写Python代码以找到两个给定日期之间的总天数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程