判断当前的输入为当年的第几天

本文介绍了一个Python函数,用于将特定日期转换为一年中的第几天。通过输入格式为'xxxx-xx-xx'的日期,函数会判断这一天是当年的第几天,并考虑到闰年的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码经过测试,个人感觉没有什么问题,但由于个人的经验不足以及眼光不够,希望各位指正程序中的不足之处。
附上代码

def check():
    '''
    输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
    '''
    print('输入一个日期,格式为(xxxx-xx-xx):', end='')
    data = input()
    if '-' not in data or data.count('-') != 2:
        print('请按照正确格式输入年月日')
        return check()
    else:
        str1 = data.split('-')  
        year = int(str1[0])  # 年
        month = int(str1[1])  # 月
        day = int(str1[2])  # 日

        if month < 1 or month > 13:
            print('月输入错误,请重新输入')
            return check()
        elif month in day31:  # 月的天数为31天
            if day > 31 or day == 0:
                print('日输入错误,该月只有31天,请重新输入')
                return check()
            else:
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month in day30:  # 月的天数为30天
            if day > 30 or day == 0:
                print('日输入错误,该月只有30天,请重新输入')
                return check()
            else:
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month == 2:
            if ((day <= 28 or (year % 4 == 0 and year % 100 != 0 and day < 29) or (year % 400 == 0 and day <= 29))):
                if year % year % 4 == 0 and year % 100 != 0 or year % 40 == 0:  # 判断是否为闰年
                    print(year366[month - 1]+day)
                else:
                    print(year365[month - 1]+day)
            else:
                print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
                return check()
if __name__ == '__main__':
    day31 = [1, 3, 5, 7, 8, 10, 12]     #天数为31天的月份
    day30 = [4, 6, 9, 11]               #天数为30天的月份
    year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]     #平年每个月结束时的天数  365天
    year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]     #闰年每个月结束时的天数  366天(2月多一天)
    check()

更新一个小错误,判断闰年的时候都是闰年,现已经更正。

def check():
    '''
    输入一个日期,格式为xxxx-xx-xx,判断这一天为当年的第几天
    '''
    print('输入一个日期,格式为(xxxx-xx-xx):', end='')
    data = input()
    if '-' not in data or data.count('-') != 2:
        print('请按照正确格式输入年月日')
        return check()
    else:
        str1 = data.split('-')  
        year = int(str1[0])  # 年
        month = int(str1[1])  # 月
        day = int(str1[2])  # 日

        if month < 1 or month > 13:
            print('月输入错误,请重新输入')
            return check()
        elif month in day31:  # 月的天数为31天
            if day > 31 or day == 0:
                print('日输入错误,该月只有31天,请重新输入')
                return check()
            else:
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month in day30:  # 月的天数为30天
            if day > 30 or day == 0:
                print('日输入错误,该月只有30天,请重新输入')
                return check()
            else:
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1] + day)
                else:
                    print(year365[month - 1] + day)
        elif month == 2:
            if ((day <= 28 or (year % 4 != 0 and day < 29) or (year % 4 == 0  and day <= 29))):
                if year % 4 == 0 :  # 判断是否为闰年
                    print(year366[month - 1]+day)
                else:
                    print(year365[month - 1]+day)
            else:
                print('日输入错误,2月最多29天,或者该年不是闰年,请重新输入')
                return check()
if __name__ == '__main__':
    day31 = [1, 3, 5, 7, 8, 10, 12]     #天数为31天的月份
    day30 = [4, 6, 9, 11]               #天数为30天的月份
    year365 = [0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]     #平年每个月结束时的天数  365天
    year366 = [0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]     #闰年每个月结束时的天数  366天(2月多一天)
    check()
在C语言中,要判断给定的年、月、日是否为当月的第几天,你可以创建一个函数来处理这个计算。以下是一个简单的步骤: 1. 首先,需要导入必要的头文件,如`stdio.h`用于输入,`time.h`用于获取当前日期作为参考。 ```c #include <stdio.h> #include <time.h> ``` 2. 创建一个辅助函数,例如 `days_in_month(int month, int year)` 来计算指定月份有多少天。注意,闰年的二月有29天,其他非闰年2月只有28天。 ```c int days_in_month(int month, int year) { switch(month) { case 2: return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28; case 4: case 6: case 9: case 11: return 30; default: return 31; } } ``` 3. 主函数中接收用户输入并调用上面的函数: ```c int main() { int day, month, year; printf("请输入年份(yyyy): "); scanf("%d", &year); printf("请输入月份(mm): "); scanf("%d", &month); printf("请输入日期(dd): "); scanf("%d", &day); // 如果输入非法,可以添加错误检查 if(month < 1 || month > 12 || day < 1 || day > days_in_month(month, year)) { printf("输入日期无效!\n"); return 1; } // 判断给定日期是不是当月的第几天 int position = day; // 初始化为给定日期,然后更新到实际位置 time_t now = time(NULL); tm* current_time = localtime(&now); // 获取当前时间结构 // 从给定日期减去开始的天数,得到当月第一天的位置 for (current_time->tm_mday -= 1; current_time->tm_mday >= 1; current_time->tm_mday--) { if (current_time->tm_mon + 1 == month && current_time->tm_year + 1900 == year) { position++; break; } } printf("给定日期 %d/%d/%d 是当年的第 %d 天.\n", month, day, year, position); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值