【问题标题】:Python: Date to Day conversionPython:日期到日期转换
【发布时间】:2016-07-07 05:34:00
【问题描述】:

我的部分中期项目是将日期(月和日)转换为数字天数(1-366,包括闰年),但我不确定如何执行此操作。我不知道如何使它工作,我也不知道如何使它,所以你只能输入有效月份(即错误,确保大写 1 月。)

以下是我到目前为止所做的。

#Months
January=0
February=31
March=60
April=91
May=121
June=152
July=182
August=213
September=244
October=274
November=305
December=335

#Title
print("Convert: Date to day and day to date.")

#user inputs a date
month=input("\nPick a Month (Make sure to capitalize the first letter): ")

day=int(input("Pick the day of the date you wish to input: "))
if day<0 or day>31:
    print("Error, no months have greater than 31 days or less than 1 day.")

请帮忙。过去几天我一直在研究这个问题,但仍然不知道该怎么做。

【问题讨论】:

  • 如果用户输入 12 月 31 日,您如何判断是第 365 天还是第 366 天?该程序是否只在闰年运行?
  • 几个提示 - 使用字典而不是变量.. 月 = {'January': 0,.... }.. 输入并检查值是否与键匹配,如果因此,从字典中获取值并添加天数。这将为您提供一个简单的工作解决方案。现在考虑如何通过额外的验证来改进它
  • 可以使用datetime等标准库吗?如果是的话,我强烈建议你看看这个帖子stackoverflow.com/questions/620305/…

标签: python string python-3.x while-loop


【解决方案1】:
_DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def _is_leap(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

def _days_in_month(year, month):
    if month == 2 and _is_leap(year):
        return 29
    return _DAYS_IN_MONTH[month]

def days(year, month, day):
    return sum([_days_in_month(year, m) for m in range(0, month)]) + day

d = days(1992, 4, 14)
print(d)

不应该是月 == 1 而不是月 == 2,因为 range() 从 0 开始,0 是一月,1 是二月?

【讨论】:

    【解决方案2】:

    代码:

    _DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    def _is_leap(year):
        return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
    
    def _days_in_month(year, month):
        if month == 2 and _is_leap(year):
            return 29
        return _DAYS_IN_MONTH[month]
    
    def days(year, month, day):
        return sum([_days_in_month(year, m) for m in range(0, month)]) + day
    
    d = days(1992, 4, 14)
    print(d)
    

    输出:

    132
    

    这很容易通过创建一些简单的函数来完成:

    1. 为一个月中的每个天数创建一个list
    2. 创建一个函数来查看是否是闰年。
    3. 创建一个函数,利用我们刚刚创建的 is_leapyear 函数来告诉您闰年或平年该月的天数。
    4. 创建一个函数,该函数将告诉我们截至该月和某日为止的天数。它的工作原理是:
      • 创建一个列表解析,返回该月之前每一天的列表。
      • 将该月过去的天数加上前几个月所有天数的总和。

    【讨论】:

      【解决方案3】:

      我在 Python 方面不是很有经验,所以不要指望它会像魅力一样发挥作用,也不要期望有一些改进的空间。但是,我在我的电脑上试过了,效果很好。我会尽量让这段代码更高效,稍后再发布。

      我不确定这段代码是否适合你的目的,但我使用了你给我的。

      您可能还想提示用户输入年份以确定代码是在寻找年份还是闰年的日期,但我不确定您是否需要,所以我暂时将其省略.在此代码中,您可以随意输入月份。

      如果您只是想打印输入日期的年份中的天数,那么您可能想尝试以下操作(此程序也会因错误答案而退出):

      #Months
      #January=0
      #February=31
      #March=60
      #April=91
      #May=121
      #June=152
      #July=182
      #August=213
      #September=244
      #October=274
      #November=305
      #December=335
      
      monthvalue = -1
      
      #Title
      print("Convert: Date to day and day to date.")
      
      #user inputs a date
      month = input("\nPick a Month (Make sure to capitalize the first letter): ")
      month = month.lower()
      month = month[:1].capitalize() + month[1:]
      
      if(month=="January"):
          monthvalue = 0
      
      if(month=="February"):
          monthvalue = 31
      
      if(month=="March"):
          monthvalue = 60
      
      if(month=="April"):
          monthvalue = 91
      
      if(month=="May"):
          monthvalue = 121
      
      if(month=="June"):
          monthvalue = 152
      
      if(month=="July"):
          monthvalue = 182
      
      if(month=="August"):
          monthvalue = 213
      
      if(month=="September"):
          monthvalue = 244
      
      if(month=="October"):
          monthvalue = 274
      
      if(month=="November"):
          monthvalue = 305
      
      if(month=="December"):
          monthvalue = 335
      
      if(monthvalue<0):
          print("Error, you have not entered a valid month.")
          xwdwa=input("\nPress any key to continue...")
          exit(0)
      
      day=int(input("Pick the day of the date you wish to input: "))
      if day<1 or day>31:
          print("Error, no months have greater than 31 days or less than 1 day.")
      
      total = monthvalue + day
      
      print("Your date is ", total, " days into a leap year.")
      exitvar = input("Press any key to continue.")
      

      【讨论】:

      • 如果有人想编辑它并提高效率并使其能够确定是否存在闰年,那就去吧。这将不胜感激。日期确定也可能存在问题...
      【解决方案4】:

      我不确定这个程序会产生什么,也看不到月份的使用情况。但是,如果您的问题是如何将用户输入的第一个字母大写,或者如果您的程序需要它,请尝试:

      month = raw_input("\nPick a Month (Make sure to capitalize the first letter): ")
      month = month[:1].capitalize() + month[1:]
      

      月份的第一个字母将大写。

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 2010-12-17
        • 2018-10-05
        • 2018-01-21
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多