欢迎关注本人博客【Together_CZ】,我是沂水寒城,本文CSDN首发地址:

https://yishuihancheng.blog.csdn.net/article/details/97028074u

python输入年月来计算该月的天数(python生成指定年份所有的天)(1)

Together_CZ

本文想要分享的内容是有关于python中日期的计算相关的内容,我在前一段时间的工作里面有一个需求就是我们需要计算每一周的目标数据,这里我最初以为的每一周就是对于每个月进行划分,每7天是一周,最后不足七天的算一周,最后跟业务端对接确认的时候发现不是这样理解的,他们要求的是按照日期中的周的划分来计算每一周的目标数据的,那么这里就出现了冲突了,需要我这边修改一下原始的计算逻辑,这一点也反映了一个道理:就是做事执行之前一定要跟需求端对接确认清楚才好哇,O(∩_∩)O哈哈~

闲话就说到这里了,其实实现需求端需要的功能并不困难,因为python中对于日期的计算已经提供了很好的封装和实现了,我们要做的就是调用相应的函数进行组合计算得到我们需要的计算结果就好了,整体的实现很简单,主要就是要考虑仔细一点,比如:闰年的时候2月是29天,一些细节问题处理好了就行了。

下面我们来看一下我的具体实现,仅作为参考,欢迎交流指导:

#!usr/bin/env python # encoding:utf-8 from __future__ import division """ __Author__:沂水寒城 功能: python生成指定年份所有的天,并计算每天属于一年的第几周和周几 """ import time import datetime month_list = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] month_day_dict = { "01": 31, "02": 28, "03": 31, "04": 30, "05": 31, "06": 30, "07": 31, "08": 31, "09": 30, "10": 31, "11": 30, "12": 31, } def string2Datetime(timestamp, format="%Y-%m-%d %H:%M:%S"): """ 把字符串转成datetime """ return datetime.datetime.strptime(timestamp, format) def judgeRunYear(year=2019): """ 判断是否是闰年 """ if (year % 100 != 0 and year % 4 == 0) or (year % 100 == 0 and year % 400 == 0): return True else: return False def generateMonthDays(month_day_dict, year="2017", month="03"): """ 生成指定年份、月份中的所有日期 """ day_num = month_day_dict[month] day_date_list = [] for i in range(1, day_num 1): one = str(i) if len(one) == 1: one = "0" one day_date_list.append(year "-" month "-" one) return day_date_list def genenrateYearDays(year=2019): """ 生成一年中所有的日期 """ res_list = [] if judgeRunYear(year=year): month_day_dict["02"] = 29 for one_mon in month_list: one_mon_day = generateMonthDays(month_day_dict, year=str(year), month=one_mon) res_list = one_mon_day return res_list def singleDateHandle(day="2019-07-19", format="%Y-%m-%d"): """ 单日处理 """ T = string2Datetime(day, format=format) weekday = T.weekday() 1 weekNum = T.isocalendar()[1] return weekDay, weekNum def genenrateYearDaysWeek(year=2019): """ 计算生成一年中每一天属于 第几周 周几 """ year_day_list = genenrateYearDays(year=year) for one_day in year_day_list: weekDay, weekNum = singleDateHandle(day=one_day) print("{0} is: {1}th week, {2}th day.".format(one_day, weekNum, weekDay)) if __name__ == "__main__": genenrateYearDaysWeek(year=2019)

接下来对上述代码进行简单的测试,结果输出如下:

2019-01-01 is: 1th week, 2th day. 2019-01-02 is: 1th week, 3th day. 2019-01-03 is: 1th week, 4th day. 2019-01-04 is: 1th week, 5th day. 2019-01-05 is: 1th week, 6th day. 2019-01-06 is: 1th week, 7th day. 2019-01-07 is: 2th week, 1th day. 2019-01-08 is: 2th week, 2th day. 2019-01-09 is: 2th week, 3th day. 2019-01-10 is: 2th week, 4th day. 2019-01-11 is: 2th week, 5th day. 2019-01-12 is: 2th week, 6th day. 2019-01-13 is: 2th week, 7th day. 2019-01-14 is: 3th week, 1th day. 2019-01-15 is: 3th week, 2th day. 2019-01-16 is: 3th week, 3th day. 2019-01-17 is: 3th week, 4th day. 2019-01-18 is: 3th week, 5th day. 2019-01-19 is: 3th week, 6th day. 2019-01-20 is: 3th week, 7th day. 2019-01-21 is: 4th week, 1th day. 2019-01-22 is: 4th week, 2th day. 2019-01-23 is: 4th week, 3th day. 2019-01-24 is: 4th week, 4th day. 2019-01-25 is: 4th week, 5th day. 2019-01-26 is: 4th week, 6th day. 2019-01-27 is: 4th week, 7th day. 2019-01-28 is: 5th week, 1th day. 2019-01-29 is: 5th week, 2th day. 2019-01-30 is: 5th week, 3th day. 2019-01-31 is: 5th week, 4th day. 2019-02-01 is: 5th week, 5th day. 2019-02-02 is: 5th week, 6th day. 2019-02-03 is: 5th week, 7th day. 2019-02-04 is: 6th week, 1th day. 2019-02-05 is: 6th week, 2th day. 2019-02-06 is: 6th week, 3th day. 2019-02-07 is: 6th week, 4th day. 2019-02-08 is: 6th week, 5th day. 2019-02-09 is: 6th week, 6th day. 2019-02-10 is: 6th week, 7th day. 2019-02-11 is: 7th week, 1th day. 2019-02-12 is: 7th week, 2th day. 2019-02-13 is: 7th week, 3th day. 2019-02-14 is: 7th week, 4th day. 2019-02-15 is: 7th week, 5th day. 2019-02-16 is: 7th week, 6th day. 2019-02-17 is: 7th week, 7th day. 2019-02-18 is: 8th week, 1th day. 2019-02-19 is: 8th week, 2th day. 2019-02-20 is: 8th week, 3th day. 2019-02-21 is: 8th week, 4th day. 2019-02-22 is: 8th week, 5th day. 2019-02-23 is: 8th week, 6th day. 2019-02-24 is: 8th week, 7th day. 2019-02-25 is: 9th week, 1th day. 2019-02-26 is: 9th week, 2th day. 2019-02-27 is: 9th week, 3th day. 2019-02-28 is: 9th week, 4th day. 2019-03-01 is: 9th week, 5th day. 2019-03-02 is: 9th week, 6th day. 2019-03-03 is: 9th week, 7th day. 2019-03-04 is: 10th week, 1th day. 2019-03-05 is: 10th week, 2th day. 2019-03-06 is: 10th week, 3th day. 2019-03-07 is: 10th week, 4th day. 2019-03-08 is: 10th week, 5th day. 2019-03-09 is: 10th week, 6th day. 2019-03-10 is: 10th week, 7th day. 2019-03-11 is: 11th week, 1th day. 2019-03-12 is: 11th week, 2th day. 2019-03-13 is: 11th week, 3th day. 2019-03-14 is: 11th week, 4th day. 2019-03-15 is: 11th week, 5th day. 2019-03-16 is: 11th week, 6th day. 2019-03-17 is: 11th week, 7th day. 2019-03-18 is: 12th week, 1th day. 2019-03-19 is: 12th week, 2th day. 2019-03-20 is: 12th week, 3th day. 2019-03-21 is: 12th week, 4th day. 2019-03-22 is: 12th week, 5th day. 2019-03-23 is: 12th week, 6th day. 2019-03-24 is: 12th week, 7th day. 2019-03-25 is: 13th week, 1th day. 2019-03-26 is: 13th week, 2th day. 2019-03-27 is: 13th week, 3th day. 2019-03-28 is: 13th week, 4th day. 2019-03-29 is: 13th week, 5th day. 2019-03-30 is: 13th week, 6th day. 2019-03-31 is: 13th week, 7th day. 2019-04-01 is: 14th week, 1th day. 2019-04-02 is: 14th week, 2th day. 2019-04-03 is: 14th week, 3th day. 2019-04-04 is: 14th week, 4th day. 2019-04-05 is: 14th week, 5th day. 2019-04-06 is: 14th week, 6th day. 2019-04-07 is: 14th week, 7th day. 2019-04-08 is: 15th week, 1th day. 2019-04-09 is: 15th week, 2th day. 2019-04-10 is: 15th week, 3th day. 2019-04-11 is: 15th week, 4th day. 2019-04-12 is: 15th week, 5th day. 2019-04-13 is: 15th week, 6th day. 2019-04-14 is: 15th week, 7th day. 2019-04-15 is: 16th week, 1th day. 2019-04-16 is: 16th week, 2th day. 2019-04-17 is: 16th week, 3th day. 2019-04-18 is: 16th week, 4th day. 2019-04-19 is: 16th week, 5th day. 2019-04-20 is: 16th week, 6th day. 2019-04-21 is: 16th week, 7th day. 2019-04-22 is: 17th week, 1th day. 2019-04-23 is: 17th week, 2th day. 2019-04-24 is: 17th week, 3th day. 2019-04-25 is: 17th week, 4th day. 2019-04-26 is: 17th week, 5th day. 2019-04-27 is: 17th week, 6th day. 2019-04-28 is: 17th week, 7th day. 2019-04-29 is: 18th week, 1th day. 2019-04-30 is: 18th week, 2th day. 2019-05-01 is: 18th week, 3th day. 2019-05-02 is: 18th week, 4th day. 2019-05-03 is: 18th week, 5th day. 2019-05-04 is: 18th week, 6th day. 2019-05-05 is: 18th week, 7th day. 2019-05-06 is: 19th week, 1th day. 2019-05-07 is: 19th week, 2th day. 2019-05-08 is: 19th week, 3th day. 2019-05-09 is: 19th week, 4th day. 2019-05-10 is: 19th week, 5th day. 2019-05-11 is: 19th week, 6th day. 2019-05-12 is: 19th week, 7th day. 2019-05-13 is: 20th week, 1th day. 2019-05-14 is: 20th week, 2th day. 2019-05-15 is: 20th week, 3th day. 2019-05-16 is: 20th week, 4th day. 2019-05-17 is: 20th week, 5th day. 2019-05-18 is: 20th week, 6th day. 2019-05-19 is: 20th week, 7th day. 2019-05-20 is: 21th week, 1th day. 2019-05-21 is: 21th week, 2th day. 2019-05-22 is: 21th week, 3th day. 2019-05-23 is: 21th week, 4th day. 2019-05-24 is: 21th week, 5th day. 2019-05-25 is: 21th week, 6th day. 2019-05-26 is: 21th week, 7th day. 2019-05-27 is: 22th week, 1th day. 2019-05-28 is: 22th week, 2th day. 2019-05-29 is: 22th week, 3th day. 2019-05-30 is: 22th week, 4th day. 2019-05-31 is: 22th week, 5th day. 2019-06-01 is: 22th week, 6th day. 2019-06-02 is: 22th week, 7th day. 2019-06-03 is: 23th week, 1th day. 2019-06-04 is: 23th week, 2th day. 2019-06-05 is: 23th week, 3th day. 2019-06-06 is: 23th week, 4th day. 2019-06-07 is: 23th week, 5th day. 2019-06-08 is: 23th week, 6th day. 2019-06-09 is: 23th week, 7th day. 2019-06-10 is: 24th week, 1th day. 2019-06-11 is: 24th week, 2th day. 2019-06-12 is: 24th week, 3th day. 2019-06-13 is: 24th week, 4th day. 2019-06-14 is: 24th week, 5th day. 2019-06-15 is: 24th week, 6th day. 2019-06-16 is: 24th week, 7th day. 2019-06-17 is: 25th week, 1th day. 2019-06-18 is: 25th week, 2th day. 2019-06-19 is: 25th week, 3th day. 2019-06-20 is: 25th week, 4th day. 2019-06-21 is: 25th week, 5th day. 2019-06-22 is: 25th week, 6th day. 2019-06-23 is: 25th week, 7th day. 2019-06-24 is: 26th week, 1th day. 2019-06-25 is: 26th week, 2th day. 2019-06-26 is: 26th week, 3th day. 2019-06-27 is: 26th week, 4th day. 2019-06-28 is: 26th week, 5th day. 2019-06-29 is: 26th week, 6th day. 2019-06-30 is: 26th week, 7th day. 2019-07-01 is: 27th week, 1th day. 2019-07-02 is: 27th week, 2th day. 2019-07-03 is: 27th week, 3th day. 2019-07-04 is: 27th week, 4th day. 2019-07-05 is: 27th week, 5th day. 2019-07-06 is: 27th week, 6th day. 2019-07-07 is: 27th week, 7th day. 2019-07-08 is: 28th week, 1th day. 2019-07-09 is: 28th week, 2th day. 2019-07-10 is: 28th week, 3th day. 2019-07-11 is: 28th week, 4th day. 2019-07-12 is: 28th week, 5th day. 2019-07-13 is: 28th week, 6th day. 2019-07-14 is: 28th week, 7th day. 2019-07-15 is: 29th week, 1th day. 2019-07-16 is: 29th week, 2th day. 2019-07-17 is: 29th week, 3th day. 2019-07-18 is: 29th week, 4th day. 2019-07-19 is: 29th week, 5th day. 2019-07-20 is: 29th week, 6th day. 2019-07-21 is: 29th week, 7th day. 2019-07-22 is: 30th week, 1th day. 2019-07-23 is: 30th week, 2th day. 2019-07-24 is: 30th week, 3th day. 2019-07-25 is: 30th week, 4th day. 2019-07-26 is: 30th week, 5th day. 2019-07-27 is: 30th week, 6th day. 2019-07-28 is: 30th week, 7th day. 2019-07-29 is: 31th week, 1th day. 2019-07-30 is: 31th week, 2th day. 2019-07-31 is: 31th week, 3th day. 2019-08-01 is: 31th week, 4th day. 2019-08-02 is: 31th week, 5th day. 2019-08-03 is: 31th week, 6th day. 2019-08-04 is: 31th week, 7th day. 2019-08-05 is: 32th week, 1th day. 2019-08-06 is: 32th week, 2th day. 2019-08-07 is: 32th week, 3th day. 2019-08-08 is: 32th week, 4th day. 2019-08-09 is: 32th week, 5th day. 2019-08-10 is: 32th week, 6th day. 2019-08-11 is: 32th week, 7th day. 2019-08-12 is: 33th week, 1th day. 2019-08-13 is: 33th week, 2th day. 2019-08-14 is: 33th week, 3th day. 2019-08-15 is: 33th week, 4th day. 2019-08-16 is: 33th week, 5th day. 2019-08-17 is: 33th week, 6th day. 2019-08-18 is: 33th week, 7th day. 2019-08-19 is: 34th week, 1th day. 2019-08-20 is: 34th week, 2th day. 2019-08-21 is: 34th week, 3th day. 2019-08-22 is: 34th week, 4th day. 2019-08-23 is: 34th week, 5th day. 2019-08-24 is: 34th week, 6th day. 2019-08-25 is: 34th week, 7th day. 2019-08-26 is: 35th week, 1th day. 2019-08-27 is: 35th week, 2th day. 2019-08-28 is: 35th week, 3th day. 2019-08-29 is: 35th week, 4th day. 2019-08-30 is: 35th week, 5th day. 2019-08-31 is: 35th week, 6th day. 2019-09-01 is: 35th week, 7th day. 2019-09-02 is: 36th week, 1th day. 2019-09-03 is: 36th week, 2th day. 2019-09-04 is: 36th week, 3th day. 2019-09-05 is: 36th week, 4th day. 2019-09-06 is: 36th week, 5th day. 2019-09-07 is: 36th week, 6th day. 2019-09-08 is: 36th week, 7th day. 2019-09-09 is: 37th week, 1th day. 2019-09-10 is: 37th week, 2th day. 2019-09-11 is: 37th week, 3th day. 2019-09-12 is: 37th week, 4th day. 2019-09-13 is: 37th week, 5th day. 2019-09-14 is: 37th week, 6th day. 2019-09-15 is: 37th week, 7th day. 2019-09-16 is: 38th week, 1th day. 2019-09-17 is: 38th week, 2th day. 2019-09-18 is: 38th week, 3th day. 2019-09-19 is: 38th week, 4th day. 2019-09-20 is: 38th week, 5th day. 2019-09-21 is: 38th week, 6th day. 2019-09-22 is: 38th week, 7th day. 2019-09-23 is: 39th week, 1th day. 2019-09-24 is: 39th week, 2th day. 2019-09-25 is: 39th week, 3th day. 2019-09-26 is: 39th week, 4th day. 2019-09-27 is: 39th week, 5th day. 2019-09-28 is: 39th week, 6th day. 2019-09-29 is: 39th week, 7th day. 2019-09-30 is: 40th week, 1th day. 2019-10-01 is: 40th week, 2th day. 2019-10-02 is: 40th week, 3th day. 2019-10-03 is: 40th week, 4th day. 2019-10-04 is: 40th week, 5th day. 2019-10-05 is: 40th week, 6th day. 2019-10-06 is: 40th week, 7th day. 2019-10-07 is: 41th week, 1th day. 2019-10-08 is: 41th week, 2th day. 2019-10-09 is: 41th week, 3th day. 2019-10-10 is: 41th week, 4th day. 2019-10-11 is: 41th week, 5th day. 2019-10-12 is: 41th week, 6th day. 2019-10-13 is: 41th week, 7th day. 2019-10-14 is: 42th week, 1th day. 2019-10-15 is: 42th week, 2th day. 2019-10-16 is: 42th week, 3th day. 2019-10-17 is: 42th week, 4th day. 2019-10-18 is: 42th week, 5th day. 2019-10-19 is: 42th week, 6th day. 2019-10-20 is: 42th week, 7th day. 2019-10-21 is: 43th week, 1th day. 2019-10-22 is: 43th week, 2th day. 2019-10-23 is: 43th week, 3th day. 2019-10-24 is: 43th week, 4th day. 2019-10-25 is: 43th week, 5th day. 2019-10-26 is: 43th week, 6th day. 2019-10-27 is: 43th week, 7th day. 2019-10-28 is: 44th week, 1th day. 2019-10-29 is: 44th week, 2th day. 2019-10-30 is: 44th week, 3th day. 2019-10-31 is: 44th week, 4th day. 2019-11-01 is: 44th week, 5th day. 2019-11-02 is: 44th week, 6th day. 2019-11-03 is: 44th week, 7th day. 2019-11-04 is: 45th week, 1th day. 2019-11-05 is: 45th week, 2th day. 2019-11-06 is: 45th week, 3th day. 2019-11-07 is: 45th week, 4th day. 2019-11-08 is: 45th week, 5th day. 2019-11-09 is: 45th week, 6th day. 2019-11-10 is: 45th week, 7th day. 2019-11-11 is: 46th week, 1th day. 2019-11-12 is: 46th week, 2th day. 2019-11-13 is: 46th week, 3th day. 2019-11-14 is: 46th week, 4th day. 2019-11-15 is: 46th week, 5th day. 2019-11-16 is: 46th week, 6th day. 2019-11-17 is: 46th week, 7th day. 2019-11-18 is: 47th week, 1th day. 2019-11-19 is: 47th week, 2th day. 2019-11-20 is: 47th week, 3th day. 2019-11-21 is: 47th week, 4th day. 2019-11-22 is: 47th week, 5th day. 2019-11-23 is: 47th week, 6th day. 2019-11-24 is: 47th week, 7th day. 2019-11-25 is: 48th week, 1th day. 2019-11-26 is: 48th week, 2th day. 2019-11-27 is: 48th week, 3th day. 2019-11-28 is: 48th week, 4th day. 2019-11-29 is: 48th week, 5th day. 2019-11-30 is: 48th week, 6th day. 2019-12-01 is: 48th week, 7th day. 2019-12-02 is: 49th week, 1th day. 2019-12-03 is: 49th week, 2th day. 2019-12-04 is: 49th week, 3th day. 2019-12-05 is: 49th week, 4th day. 2019-12-06 is: 49th week, 5th day. 2019-12-07 is: 49th week, 6th day. 2019-12-08 is: 49th week, 7th day. 2019-12-09 is: 50th week, 1th day. 2019-12-10 is: 50th week, 2th day. 2019-12-11 is: 50th week, 3th day. 2019-12-12 is: 50th week, 4th day. 2019-12-13 is: 50th week, 5th day. 2019-12-14 is: 50th week, 6th day. 2019-12-15 is: 50th week, 7th day. 2019-12-16 is: 51th week, 1th day. 2019-12-17 is: 51th week, 2th day. 2019-12-18 is: 51th week, 3th day. 2019-12-19 is: 51th week, 4th day. 2019-12-20 is: 51th week, 5th day. 2019-12-21 is: 51th week, 6th day. 2019-12-22 is: 51th week, 7th day. 2019-12-23 is: 52th week, 1th day. 2019-12-24 is: 52th week, 2th day. 2019-12-25 is: 52th week, 3th day. 2019-12-26 is: 52th week, 4th day. 2019-12-27 is: 52th week, 5th day. 2019-12-28 is: 52th week, 6th day. 2019-12-29 is: 52th week, 7th day. 2019-12-30 is: 1th week, 1th day. 2019-12-31 is: 1th week, 2th day.

我们队2019年全年的每一天都做了一个计算,上面是所有的输出结果,那么如何来验证计算的是否正确呢?这里我们以今天为例进行验证,从电脑右下角的日期得知今天是周三:

python输入年月来计算该月的天数(python生成指定年份所有的天)(2)

我们代码的计算结果如下:

python输入年月来计算该月的天数(python生成指定年份所有的天)(3)

核对无误,至于今天是一年的第几周这个感兴趣的话也是可以从电脑右下角的日历里面数一数的,我数了一下的确是一年的第31周,你数过了吗?

,