Python 使用已定义的函数实现某些方程

Python 使用已定义的函数实现某些方程,python,Python,我需要您的帮助,我正在尝试编写一个PYTHON代码,该代码使用一个名为calcExcelDate的已定义函数来实现给定日期的excel日期等效公式,指定为: 年份(1900到9999之间的4位整数) 月份(1到12之间的1或2位整数) 天(1到31之间的1或2位整数,取决于月份) 我定义了这个函数,并使用3个循环来指定上述给定的年、月和日范围。但如何使用该函数实现以下等式: y_2 = year - 1900 em = math.floor((14 - month) / 12) y_3 =

我需要您的帮助,我正在尝试编写一个PYTHON代码,该代码使用一个名为calcExcelDate的已定义函数来实现给定日期的excel日期等效公式,指定为:

  • 年份(1900到9999之间的4位整数)
  • 月份(1到12之间的1或2位整数)
  • 天(1到31之间的1或2位整数,取决于月份)
我定义了这个函数,并使用3个循环来指定上述给定的年、月和日范围。但如何使用该函数实现以下等式:

y_2 = year - 1900
em = math.floor((14 - month) / 12)
y_3 = y_2 - em
m_2 = month + 12 * em
I_ = 1 + min(y_3, 0) + math.floor(y_3 / 4) - math.floor(y_3 / 100) + math.floor((y_3 + 300) / 400)
d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6)
d_2 = day + y_3 * 365 + I_ + d_1
到目前为止,我的代码是:

import time
import math


DAYS = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
FULL_MONTHS = ("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December")


mm_ = tuple(str(date) for date in range(2013-10-17, 2100-01-01))    # a tuple of character strings containing dates in the date range Excel supports,
                                                                    # such as ("2013-Oct-17", "2100-01-01")
print mm_


def calcExcelDate(year, month, day):

  for year in range(1900, 10000):
    for month in range(1, 13):
      for day in range(1, 32):
        y_2 = year - 1900
        em = math.floor((14 - month) / 12)
        y_3 = y_2 - em
        m_2 = month + 12 * em
        I_ = 1 + min(y_3, 0) + math.floor(y_3 / 4) - math.floor(y_3 / 100)
                           + math.floor((y_3 + 300) / 400)
        d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6)
        d_2 = day + y_3 * 365 + I_ + d_1


xx_ = calcExcelDate(2010, 10, 1)
print xx_

对于这种情况,最好使用标准库作为希望处理的日期和时间的接口,然后根据需要将其转换为Excel序列日期

以下代码显示了如何在模块中将日期转换为Excel日期

使用
datetime
的优点是,您有一个标准接口,其中包含各种方法,用于将字符串解析为日期或时间:

my_datetime = datetime.datetime.strptime('2013-01-23', '%Y-%m-%d')

您希望calcExcelDate(2010,10,1)的输出是什么?我认为您不需要遍历所有的
/
/
(特别是因为这只会覆盖传递到函数中的
/
/
日)。所以删除三个for循环可能是第一步,只需留下方程,但我不知道你想要返回什么。
d_2
是否包含您想要的最终答案?同意。“功能”一词通常意味着输入和输出之间的某种关系。看起来输入将是某个日期。你想要什么样的输出?为了检查您的代码是否正在计算您期望的值,您手工完成了哪些具体示例?
my_datetime = datetime.datetime.strptime('2013-01-23', '%Y-%m-%d')