Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中获取过去日期戳和现在日期之间的日期_Python - Fatal编程技术网

在python中获取过去日期戳和现在日期之间的日期

在python中获取过去日期戳和现在日期之间的日期,python,Python,语言Python 我想知道是否有人能帮我打印一些日期 我尝试创建一个循环,在其中我传入一个日期,比如2017年1月1日,然后循环将输出从那时到现在的每个月的第一天和最后一天 示例 01/01/2017 31/01/2017 01/02/2017 28/02/2017 等 任何帮助都将不胜感激希望这会有所帮助 代码: from datetime import date from dateutil.relativedelta import relativedelta from calendar i

语言Python

我想知道是否有人能帮我打印一些日期

我尝试创建一个循环,在其中我传入一个日期,比如2017年1月1日,然后循环将输出从那时到现在的每个月的第一天和最后一天

示例

01/01/2017
31/01/2017
01/02/2017
28/02/2017

任何帮助都将不胜感激

希望这会有所帮助

代码:

from datetime import date
from dateutil.relativedelta import relativedelta
from calendar import monthrange

d1 = date(2018, 2, 26)  
d2 = date.today()

def print_month_day_range(date):
    first_day = date.replace(day = 1)
    last_day = date.replace(day = monthrange(date.year, date.month)[1])
    print (first_day.strftime("%d/%m/%Y"))
    print (last_day.strftime("%d/%m/%Y"))

print_month_day_range(d1)
while (d1 < d2):
    d1 = d1 + relativedelta(months=1)
    print_month_day_range(d1)
01/02/2018
28/02/2018
01/03/2018
31/03/2018
...
01/07/2018
31/07/2018

您可以使用日历模块。代码如下:

import datetime
from calendar import monthrange

strt_date = '01/1/2017'
iter_year = datetime.datetime.strptime(strt_date, '%m/%d/%Y').year
iter_month = datetime.datetime.strptime(strt_date, '%m/%d/%Y').month
cur_year = datetime.datetime.today().year
cur_month = datetime.datetime.today().month

while cur_year > iter_year or (cur_year == iter_year and iter_month <= cur_month):
    number_of_days_in_month = monthrange(iter_year, iter_month)[1]
    print '/'.join([str(iter_month) , '01', str(iter_year)])
    print '/'.join([str(iter_month), str(number_of_days_in_month), str(iter_year)])
    if iter_month == 12:
        iter_year += 1
        iter_month = 1
    else:
        iter_month += 1
导入日期时间
从日历导入monthrange
strt_日期='2017年1月1日'
iter_year=datetime.datetime.strtime(strt_日期,'%m/%d/%Y')。年
iter_month=datetime.datetime.strtime(strt_日期,'%m/%d/%Y')。month
cur_year=datetime.datetime.today().year
cur_month=datetime.datetime.today().month

当cur_year>iter_year或(cur_year==iter_year和iter_month时,只要您给出的第一个日期始终是该月的第一天,这可能会起作用

from datetime import datetime
from datetime import timedelta

date_string = '01/01/2017'
date = datetime.strptime(date_string, '%d/%m/%Y').date()
today = datetime.now().date()

months = range(1,13)
years = range(date.year, today.year + 1)

for y in years:
    for m in months:
        new_date = date.replace(month=m, year=y)
        last_day = new_date - timedelta(days=1)
        if (date < new_date) & (new_date <= today):
            print last_day.strftime('%d/%m/%Y')
            print new_date.strftime('%d/%m/%Y')

        elif (date <= new_date) & (new_date <= today):
            print new_date.strftime('%d/%m/%Y')
从日期时间导入日期时间
从日期时间导入时间增量
日期字符串='01/01/2017'
date=datetime.strTime(日期字符串,'%d/%m/%Y')。日期()
今天=datetime.now().date()
月份=范围(1,13)
年份=范围(date.year,today.year+1)
对于y年:
以月为单位的m:
新日期=日期。替换(月=m,年=y)
最后一天=新日期-时间增量(天=1)

如果(日期
import datetime

def first_day_of_month(any_day):
    first_month = any_day.replace(day=1)
    return first_month

def last_day_of_month(any_day):
    next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
    return next_month - datetime.timedelta(days=next_month.day)

for month in range(1, 13):
    print first_day_of_month(datetime.date(2017, month, 1))
    print last_day_of_month(datetime.date(2017, month, 1))

是的,你为什么需要这个?要用selenium建立一个日期列表插入网页,你可以硬编码,但这样做会证明它的未来