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:天真的方法

在這種方法中,天真的解決方案將從 date_1 開始,它將繼續計算天數,直到到達 date_2。該解決方案將需要超過 O(1) 次。這是一個計算 date_1 之前的總天數的簡單解決方案,這意味着它將計算從 00/00/0000 到 date_1 的總天數,然後它將計算 date_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 日期時間模塊

在這個方法中,我們將看到如何使用 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 代碼來查找兩個給定日期之間的總天數的兩種不同方法。


原創文章,作者:OSARI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128574.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
OSARI的頭像OSARI
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 爬蟲是一種程序

    爬蟲是一種程序,用於自動獲取互聯網上的信息。本文將從如下多個方面對爬蟲的意義、運行方式、應用場景和技術要點等進行詳細的闡述。 一、爬蟲的意義 1、獲取信息:爬蟲可以自動獲取互聯網上…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟件。在VB中,有許多不同的方法可以運行程序,包括編譯器、發布程序以及命令行。在本文中…

    編程 2025-04-29
  • Python一元二次方程求解程序

    本文將詳細闡述Python一元二次方程求解程序的相關知識,為讀者提供全面的程序設計思路和操作方法。 一、方程求解 首先,我們需要了解一元二次方程的求解方法。一元二次方程可以寫作: …

    編程 2025-04-29
  • 如何使用GPU加速運行Python程序——以CSDN為中心

    GPU的強大性能是眾所周知的。而隨着深度學習和機器學習的發展,越來越多的Python開發者將GPU應用於深度學習模型的訓練過程中,提高了模型訓練效率。在本文中,我們將介紹如何使用G…

    編程 2025-04-29

發表回復

登錄後才能評論