简体   繁体   English

Python 日历,计算月初前一周的空闲天数

[英]Python calendar, counting empty days of the week before the first of the month

I did something like this:我做了这样的事情:

import datetime
nb_blank_days = (int(datetime.date(year, month, 1).strftime('%w'))+6)%7

But I doesn't looks very pythonic.但我看起来不是很pythonic。 Any help?有什么帮助吗?

The goal is to say that because the first of July is a friday, there is 4 blanks in the week before displaying the first.目标是说因为 7 月 1 日是星期五,所以在显示第一个之前的一周有 4 个空白。

How about just刚刚怎么样

datetime.date(year, month, 1).weekday()

Since 0 is Monday, I think this does what you want.由于0是星期一,我认为这可以满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python本月的日历周列表 - Calendar week list of the current month in Python 日历无法在第一周显示前几个月的天数 - Calendar Can't Show Previous Months Days In First Week 如何在 Python 中创建当月星期几的列表? - How to create a list of days of the week of the current month in Python? Python 倒计时但在年、月、周、日、时、分、秒 - Python Countdown but in Year, Month, Week, Days, Hours, Minutes, Sec 是否有相当于 Java 的 Calendar.DAY_OF_WEEK_IN_MONTH 的 Python? - Is there a Python equivalent of Java's Calendar.DAY_OF_WEEK_IN_MONTH? 日历周结束天数 - Calendar week end days issue 在每个月的第一个星期一的活动开始前7天显示警报 - Display alert 7 days before event on first monday of every month 使用Python查找一年中的星期,该星期在星期六和第一周结束,其中至少包含4天 - Finding week of year with week ending on Saturday and first week containing atleast 4 days, in python 我需要编写一个 function 来打印给定月份的一周,其中 arguments 周数,开始日期和月份中的天数(Python) - I need to write a function to print out a week of a given month, with the arguments week number, start day and days in the month (Python) Python从datetime列表中获取一个月中第一天的列表 - Python obtaining list of first days in a month from list of datetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM