社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在python中获取日期介于两个日期之间的周天数

Santhosh • 5 年前 • 1825 次点击  

因为我在网站上搜索了python中的工作日数,但我也需要工作日的日期。

我的 输入 将:

 start date = 01-03-2019
 end_date = 15-03-2019
 days = monday , tuesday

预期 产量 :

我需要打印星期一和星期二的天数和日期。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41081
 
1825 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Chris
Reply   •   1 楼
Chris    6 年前

使用 pandas.to_datetime pandas.date_range :

import pandas as pd

start_date = '01-03-2019'
end_date = '15-03-2019'
days = ['Monday', 'Tuesday']

dates = pd.to_datetime([start_date, end_date], format='%d-%m-%Y')
pd.date_range(dates[0],dates[1],freq='1d').day_name().value_counts()[days]
Monday     2
Tuesday    2
dtype: int64
Arkistarvh Kltzuonstev
Reply   •   2 楼
Arkistarvh Kltzuonstev    6 年前

试试这个:

start_date = '01-03-2019'   # Considering 03 is the month, 01 is the day
end_date = '15-03-2019'    # Considering 03 is the month, 15 is the day
start_date = [int(i) for i in start_date.split("-")]
end_date = [int(i) for i in end_date.split("-")]
days = 'monday' , 'tuesday'
from datetime import timedelta, date
start_date = date(start_date[-1], start_date[1], start_date[0])
end_date = date(end_date[-1], end_date[1], end_date[0])
# Now check in the range of start_date and end_date with condition .weekday() Monday is 0 and Tuesday is 1.
def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)
for single_date in daterange(start_date, end_date):
    if single_date.weekday() ==0:
        print("Monday : ", single_date)
    if single_date.weekday() == 1:
        print("Tuesday : ", single_date)
Navi
Reply   •   3 楼
Navi    6 年前
start_date = datetime.strptime(start_date, '%Y-%m-%d')
end_date = datetime.strptime(send_date, '%Y-%m-%d')

step = timedelta(days=1)
while start_date <= end_date:
    for day in days:
        if day == calendar.day_name[start_date.date().weekday()]:
            print calendar.day_name[start_date.date()]
        start_date += step